3

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

Pablo
  • 1,041
  • 8
  • 23

1 Answers1

2

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.

Luis
  • 11,978
  • 3
  • 27
  • 35
  • Thank you, it wasn't the same as I asked but it was very useful. – Pablo Nov 30 '12 at 10:39
  • Then, what did you ask? The example above converts 5 pixels to meters in real world. Isn't that your question? – Luis Nov 30 '12 at 10:53
  • I wanted to zoom the map to a scale of 5pixels:20meters, but it doesn't matter, I can use your answer for my application purposes. Thanks. – Pablo Nov 30 '12 at 11:31
  • Now I got it. But that will not be possibe, because google maps only zoom in a power of 2 steps. So you will need to choose from the zoom level closest to your scale requiremets (but it will not be exactly the value you would like). – Luis Nov 30 '12 at 11:47