Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google map zoom in between LatLng Bounds

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

(7) android Zoom-to-Fit All Markers on Google Map v2

like image 238
Miriana Itani Avatar asked Jul 08 '16 10:07

Miriana Itani


People also ask

How do I zoom in smaller increments on Google Maps?

You can change the zoom level of the map using simple steps. Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'. Step 3 click save map and see the changes.

Can you zoom in on Google Maps?

Zoom in the mapYou can zoom in and out of the map with one hand. Double tap a spot on the map, and then: Drag down to zoom in. Drag up to zoom out.

How do you calculate zoom level on a map?

Convert latitude, longitude to spherical mercator x, y. Get distance between your two points in spherical mercator. The equator is about 40m meters long projected and tiles are 256 pixels wide, so the pixel length of that map at a given zoom level is about 256 * distance/40000000 * 2^zoom.


2 Answers

It sounds like you use a function to only zoom out when necessary, but not to zoom in as close as possible. Can you post the part that you use to animate the camera as well?

If you simply use

LatLngBounds bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 20));

it should work. And make sure to call it and do not wrap it in a check that looks if the bounds are already visible.

like image 161
Björn Kechel Avatar answered Oct 12 '22 08:10

Björn Kechel


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));
like image 30
Sreehari Avatar answered Oct 12 '22 06:10

Sreehari