Automatically adjust zoom to accommodate all marker in a google map

Viewed 48552

Using the latest version of Google maps. How to add markers using longitude and latitude and automatically adjust the zoom level of the map to include all the markers using JavaScript?

3 Answers

The following did the trick for me

map.fitBounds(bounds);


 // add a zoom to the map


var listener = google.maps.event.addListener(map, 'idle', function()
        {
          if(map.getZoom() > 16)
            map.setZoom(17);
          google.maps.event.removeListener(listener);
        });

Set your desired zoom in the setZoom() method. The larger the value, the wider it shows location details, the smaller the value, it throttles and shrinks the location details

Related