Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android (MapView): How to set Zoom level of 4 miles in the mapview?

I have a latitude and longitude(GeoPoint) which is the center of map. I want to set zoom level of 4miles distance from latitude and longitude that I have.

Thanks in Advance,

like image 579
Mani Avatar asked Apr 08 '11 12:04

Mani


1 Answers

SOLVED :

from Androd google API : zoomlevel sets the zoomlevel of the map. The value will be clamped to be between 1 and 21 inclusive, though not all areas have tiles at higher zoom levels. This just sets the level of the zoom directly; Parameters: zoomLevel - At zoomLevel 1, the equator of the earth is 256 pixels long. Each successive zoom level is magnified by a factor of 2. Returns: the new zoom level, between 1 and 21 inclusive.

So given the Equator E = 40075 Km or 21.638,8 miles given the distance to zoom D = 20 km or miles

the zoom level : L = log2(E/D)+1

This is the simple method I've used remember to set E= 21.638,8 if you want to use the miles...

public static byte zoomLevel (double distance){
    byte zoom=1;
    double E = 40075;
    Log.i("Astrology", "result: "+ (Math.log(E/distance)/Math.log(2)+1));
    zoom = (byte) Math.round(Math.log(E/distance)/Math.log(2)+1);
    // to avoid exeptions
    if (zoom>21) zoom=21;
    if (zoom<1) zoom =1;

    return zoom;
}
like image 194
Matteo Avatar answered Oct 21 '22 11:10

Matteo