I have two markers on a map that should both show at the same time.
This is achieved through :
final LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker markerTemp : markers) {
builder.include(markerTemp.getPosition());
}
this works fine. However, when the markers are close to each other the map is very zoomed out. This is how it happens: 1- I choose two locations that are close to each other (zoom is good) 2- I choose two locations that are far from each other (zoom is good) 3- I choose two locations which are close to each other (zoom stays the same and the markers over lap).
I have checked so many links including:
(1) https://stackoverflow.com/questions/34247347/initialize-google-map-to-zoom-to-bounds (2) bounds google maps
(3) Setting max zoom level in google maps android api v2 (4) Android map v2 zoom to show all the markers
(5) Adjusting google map (api v2) zoom level in android (6) Set a max zoom level on LatLngBounds builder
You can get distance between two location through the below function and find the zoom level based on that.
Method to find direct distance between two location
location1.distanceTo(location2);
Now to calculate the radius , use the following. dummy_radius
will be half of above value
double circleRad = dummy_radius*1000;//multiply by 1000 to make units in KM
private int getZoomLevel(double radius){
double scale = radius / 500;
return ((int) (16 - Math.log(scale) / Math.log(2)));
}
float zoomLevel = getZoomLevel(circleRad);
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(LatLon, zoomLevel));