Google Maps API v3: Can I setZoom after fitBounds?

前端 未结 23 2323
执笔经年
执笔经年 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:52
    google.maps.event.addListener(marker, 'dblclick', function () {
        var oldZoom = map.getZoom(); 
        map.setCenter(this.getPosition());
        map.setZoom(parseInt(oldZoom) + 1);
    });
    
    0 讨论(0)
  • 2020-11-27 09:53

    Had the same problem, needed to fit many markers on the map. This solved my case:

    1. Declare bounds
    2. Use scheme provided by koderoid (for each marker set bounds.extend(objLatLng))
    3. Execute fitbounds AFTER map is completed:

      google.maps.event.addListenerOnce(map, 'idle', function() { 
          map.fitBounds( bounds );
      });
      
    0 讨论(0)
  • 2020-11-27 09:54

    Please try this:

    map.fitBounds(bounds);
    
    // CHANGE ZOOM LEVEL AFTER FITBOUNDS
    zoomChangeBoundsListener = google.maps.event.addListenerOnce(map, 'bounds_changed', function(event) {
      if (this.getZoom()){
        this.setZoom(15);
      }
    });
    setTimeout(function(){
      google.maps.event.removeListener(zoomChangeBoundsListener)
    }, 2000);
    
    0 讨论(0)
  • 2020-11-27 09:57

    If 'bounds_changed' is not firing correctly (sometimes Google doesn't seem to accept coordinates perfectly), then consider using 'center_changed' instead.

    The 'center_changed' event fires every time fitBounds() is called, although it runs immediately and not necessarily after the map has moved.

    In normal cases, 'idle' is still the best event listener, but this may help a couple people running into weird issues with their fitBounds() calls.

    See google maps fitBounds callback

    0 讨论(0)
  • 2020-11-27 09:59

    I use:

    gmap.setZoom(24); //this looks a high enough zoom value
    gmap.fitBounds(bounds); //now the fitBounds should make the zoom value only less
    

    This will use the smaller of 24 and the necessary zoom level according to your code, however it probably changes the zoom anyway and doesn't care about how much you zoomed out.

    0 讨论(0)
  • 2020-11-27 09:59

    this work's for me with API v3 but with setting fixed zoom:

    var bounds = new google.maps.LatLngBounds();
    // extend bounds with each point
    
    gmap.setCenter(bounds.getCenter()); 
    gmap.setZoom( 6 );
    
    0 讨论(0)
提交回复
热议问题