Google Maps v3 fitBounds() Zoom too close for single marker

前端 未结 17 2069
别那么骄傲
别那么骄傲 2020-12-12 23:28

Is there a way to set a max zoom level for fitBounds()? My problem is that when the map is only fed one location, it zooms in as far as it can go, which really

17条回答
  •  时光说笑
    2020-12-13 00:09

    I solved with this chunk, since Google Maps V3 is event driven:

    you can tell the API to set back the zoom to a proper amount when the zoom_changed event triggers:

    var initial = true
    google.maps.event.addListener(map, "zoom_changed", function() {
        if (intial == true){
           if (map.getZoom() > 11) {
             map.setZoom(11);
             intial = false;
           }
        }
    }); 
    

    I used intial make the map not zooming too much loading when the eventual fitBounds permorfed, without it any zoom event over 11 would be impossible for the user.

提交回复
热议问题