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

前端 未结 17 2122
别那么骄傲
别那么骄傲 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-12 23:56

    I have soulution based on limiting max zoom when fitting bounds. Works for me (tested on Win 7 - IE 9, FF 13, Chrome 19):

    // When fitting bounds:
    var bounds = new google.maps.LatLngBounds();
    // ...
    // extend bounds as you like
    // ..
    
    // now set global variable when fitting bounds
    window.fittingBounds = true;
    map.fitBounds(bounds);
    window.fittingBounds = false;
    
    
    // attach this event listener after map init
    google.maps.event.addListener(map, 'zoom_changed', function() {
        // set max zoom only when fitting bounds
        if (window.fittingBounds && map.getZoom() > 16) {
            this.setZoom(16);
        }
    });
    

提交回复
热议问题