Google Maps v3: Enforcing min. zoom level when using fitBounds

前端 未结 8 2076
攒了一身酷
攒了一身酷 2020-12-12 15:02

I\'m drawing a series of markers on a map (using v3 of the maps api).

In v2, I had the following code:

  bounds = new GLatLngBounds();

  ... loop th         


        
8条回答
  •  无人及你
    2020-12-12 15:29

    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 (initial == true){
           if (map.getZoom() > 11) {
             map.setZoom(11);
             initial = false;
           }
        }
    }); 
    

    I used initial to make the map not zooming too much when the eventual fitBounds is permorfed, but to let the user zoom as much as he/she wants. Without the condition any zoom event over 11 would be possible for the user.

提交回复
热议问题