Automatically adjust zoom to accommodate all marker in a google map

前端 未结 3 1194
后悔当初
后悔当初 2020-12-25 10:17

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

3条回答
  •  甜味超标
    2020-12-25 10:55

    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

提交回复
热议问题