Android (MapView): How to set Zoom level of 4 mile

2020-06-17 04:07发布

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,

3条回答
Anthone
2楼-- · 2020-06-17 04:47

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;
}
查看更多
狗以群分
3楼-- · 2020-06-17 04:48

Play with the .setZoom(int); parameter of your map. i think its around 15:

so myMap.setZoom(15);

查看更多
迷人小祖宗
4楼-- · 2020-06-17 04:52

You can use the setzoom()-function to do so. The code below can be found in this example.

mc = mapView.getController();
String coordinates[] = {"1.352566007", "103.78921587"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);

p = new GeoPoint(
    (int) (lat * 1E6), 
    (int) (lng * 1E6));

mc.animateTo(p);
mc.setZoom(17); 
查看更多
登录 后发表回答