Asaf Nevo
Asaf Nevo

Reputation: 11688

Android MapView zoom to a radius

in my application i have a MapView.

i want to change the zoom of the map according to the radius the user set. i've found the answer in this link, but the problem is that the function consider only the screen's width.. i want to height to be consider too.

for instance: if i want to see radius of 1km - the function in the link will show me a width of 1km but not a height of 1km.. it is really important for me that it will show it on both width and height even if when height is 1km the width is 1.5km

how can i do it ?

Upvotes: 4

Views: 2713

Answers (1)

Syed Raza Mehdi
Syed Raza Mehdi

Reputation: 4117

Here is the working code and with accurate result.

1) Make a circle on object with desired radius

Circle circle = mGoogleMap.addCircle(new CircleOptions().center(new LatLng(latitude, longitude)).radius(getRadiusInMeters()).strokeColor(Color.RED));           
        circle.setVisible(true);
        getZoomLevel(circle);

2) Pass that object to this function and set the zoom level Here's a link

public int getZoomLevel(Circle circle) {
if (circle != null){
    double radius = circle.getRadius();
    double scale = radius / 500;
    zoomLevel =(int) (16 - Math.log(scale) / Math.log(2));
}
return zoomLevel;
}

Upvotes: 4

Related Questions