Pablo
Pablo

Reputation: 1091

How to scale a MapView from Pixels to Meters

I'm making an Android application which uses Google Maps API, and I want to scale a MapView to X_pixels:X_meters.

For example, 5 pixels of the MapView in my screen, 20 meters on reality.

Is that possible?

Thx

Upvotes: 3

Views: 2363

Answers (1)

Luis
Luis

Reputation: 12058

Use the following code:

    int nPixles = 5; //number of pixels
    GeoPoint g0 = mapview.getProjection().fromPixels(0, mapview.getHeight()/2);
    GeoPoint g1 = mapview.getProjection().fromPixels(nPixles, mapview.getHeight()/2);
    float[] results = new float[1];
    Location.distanceBetween(g0.getLatitudeE6()/1E6, g0.getLongitudeE6()/1E6, g1.getLatitudeE6(), g1.getLongitudeE6()/1E6, results);
    float distanceInMeters = results[0];

This calculates distance in meters for latitude level at screen center. Because earth is spheric distance vary from bottom to the top of screen. This is mostly noticed with low zoom levels.

Regards.

Upvotes: 2

Related Questions