I want to zoom
the Google map
to a specific radius in miles say 10 miles/20 miles/30 miles etc as per my condition in android.
What I need is to draw a circle of specific radius (10/20/30.. miles) from the current lat long point and zoom the map to that particular miles for which i have draw the circle with radius.
I am able to point my current position, Draw the circle. But I am not able to zoom the map to desired radius mile only(circle should be focus on the screen with specified miles with my current position as center).
Currently I am using the following code to zoom:
gooMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(selectedLat, selectedLong), 15));
gooMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
Log.e("Circle Lat Long:", selectedLat + ", " + selectedLong);
circle.remove();
circle = gooMap.addCircle(new CircleOptions()
.center(new LatLng(selectedLat, selectedLong))
.radius(iMiles * 1609.34) // Converting Miles into Meters...
.strokeColor(Color.RED)
.strokeWidth(5));
circle.isVisible();
I knew that giving the zoom level to 15 will not zoom the map to the desire miles. But I need to zoom the map to desire miles radius. how can i achieve it. Can anyone can help me for the same. Edit:- Image added explains what I need in details. Image_1:- When I set my radius to say 10 miles then the map should zoom as shown. Image_2 When I set my radius to say 50 miles then the map should zoom as shown. Please see the difference in the map places to analysis the zoom level I need. Thanks in advance.
I got the solution for the same, Here is the one:
// Zoom in, animating the camera.
double iMeter = iMiles * 1609.34;
circle.remove();
circle = gooMap.addCircle(new CircleOptions()
.center(new LatLng(selectedLat, selectedLong))
.radius(iMeter) // Converting Miles into Meters...
.strokeColor(Color.RED)
.strokeWidth(5));
circle.isVisible();
float currentZoomLevel = getZoomLevel(circle);
float animateZomm = currentZoomLevel + 5;
Log.e("Zoom Level:", currentZoomLevel + "");
Log.e("Zoom Level Animate:", animateZomm + "");
gooMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(selectedLat, selectedLong), animateZomm));
gooMap.animateCamera(CameraUpdateFactory.zoomTo(currentZoomLevel), 2000, null);
Log.e("Circle Lat Long:", selectedLat + ", " + selectedLong);
And our method that calculate the zoom level as per device is as follows:
public float getZoomLevel(Circle circle) {
float zoomLevel=0;
if (circle != null){
double radius = circle.getRadius();
double scale = radius / 500;
zoomLevel =(int) (16 - Math.log(scale) / Math.log(2));
}
return zoomLevel +.5f;
}
You need to find the distance across the screen when the view gets refreshed. I'm not too familiar with the new APIs and views but something like this should work:
LatLngBounds bounds = map.getProjection().getVisibleRegion().latLngBounds;
double llNeLat = bounds.northeast.latitude;
double llSwLat = bounds.southwest.latitude;
double llNeLng = bounds.northeast.longitude;
double llSwLng = bounds.southwest.longitude;
float results[] = new float[5];
Location.distanceBetween(llNeLat, llNeLng, llSwLat, llSwLng, results);
float radius = results[0];
// radius is distance top right to bottom left on screen in metres
// so use maybe 3/4 of this, then conert your miles to that value
// in metres
then add that value to the addCircle invocation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With