How to scale a MapView from Pixels to Meters

2019-07-15 11:47发布

问题:

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

回答1:

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.