Google Maps API v3: Can I setZoom after fitBounds?

前端 未结 23 2489
执笔经年
执笔经年 2020-11-27 09:35

I have a set of points I want to plot on an embedded Google Map (API v3). I\'d like the bounds to accommodate all points unless the zoom level is too low (i.e., zoomed out

23条回答
  •  庸人自扰
    2020-11-27 09:48

    I use this to ensure the zoom level does not exceed a set level so that I know satellite images will be available.

    Add a listener to the zoom_changed event. This has the added benefit of controlling the zoom control on the UI also.

    Only execute setZoom if you need to, so an if statement is preferable to Math.max or to Math.min

       google.maps.event.addListener(map, 'zoom_changed', function() { 
          if ( map.getZoom() > 19 ) { 
            map.setZoom(19); 
          } 
        });
        bounds = new google.maps.LatLngBounds( ... your bounds ... )
        map.fitBounds(bounds);
    

    To prevent zooming out too far:

       google.maps.event.addListener(map, 'zoom_changed', function() { 
          if ( map.getZoom() < 6 ) { 
            map.setZoom(6); 
          } 
        });
        bounds = new google.maps.LatLngBounds( ... your bounds ... )
        map.fitBounds(bounds);
    

提交回复
热议问题