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

前端 未结 17 2071
别那么骄傲
别那么骄傲 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:59

    After calling fitBounds() method, try to setup zoom level again. It will force the map to be at that zoom level whilst being centered at the right place.

    0 讨论(0)
  • 2020-12-13 00:00

    You can setup your map with maxZoom in the MapOptions (api-reference) like this:

    var map = new google.maps.Map(document.getElementById("map"), { maxZoom: 10 });
    

    This would keep the map from zooming any deeper when using fitBounds() and even removes the zoom levels from the zoom control.

    0 讨论(0)
  • 2020-12-13 00:04
    var map = new google.maps.Map(document.getElementById("map"), { maxZoom: 10 });
    

    Using the MaxZoom option works best for not zooming to close on to the marks you have.

    0 讨论(0)
  • 2020-12-13 00:08

    Once you've added all of the real bounds add these lines

    var offset = 0.002;     
    var center = bounds.getCenter();                            
    bounds.extend(new google.maps.LatLng(center.lat() + offset, center.lng() + offset));
    bounds.extend(new google.maps.LatLng(center.lat() - offset, center.lng() - offset));
    

    it get the center of the real bounds then adds two additional points one to the northeast and one to the southwest of you center

    This effectively sets the minimum zoom, change the value of offset to increase or decrease the zoom

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题