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

前端 未结 8 2015
攒了一身酷
攒了一身酷 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:33

    Without trying it, I'd say you should be able to do it just by having fitBounds() before you get the zoom level, i.e.

    map.fitBounds(bounds);
    var zoom = map.getZoom();
    map.setZoom(zoom > 6 ? 6 : zoom);
    

    If you did try that and it didn't work, you can setup your map with minZoom in the MapOptions (api-reference) like this:

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

    This would keep the map from zooming any further out when using fitBounds().

    0 讨论(0)
  • 2020-12-12 15:33

    I found the following to work quite nicely. It is a variant on Ryan's answer to https://stackoverflow.com/questions/3334729/.... It guarantees to show an area of at least two times the value of offset in degrees.

    const center = bounds.getCenter()
    const offset = 0.01
    const northEast = new google.maps.LatLng(
        center.lat() + offset,
        center.lng() + offset
    )
    const southWest = new google.maps.LatLng(
        center.lat() - offset,
        center.lng() - offset
    )
    const minBounds = new google.maps.LatLngBounds(southWest, northEast)
    map.fitBounds(bounds.union(minBounds))
    
    0 讨论(0)
提交回复
热议问题