Google Maps API v3: Can I setZoom after fitBounds?

前端 未结 23 2538
执笔经年
执笔经年 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:44

    I've found a solution that does the check before calling fitBounds so you don't zoom in and suddenly zoom out

    var bounds = new google.maps.LatLngBounds();
    
    // extend bounds with each point
    
    var minLatSpan = 0.001;
    if (bounds.toSpan().lat() > minLatSpan) {
        gmap.fitBounds(bounds); 
    } else {
        gmap.setCenter(bounds.getCenter());
        gmap.setZoom(16);
    }
    

    You'll have to play around with the minLatSpan variable a bit to get it where you want. It will vary based on both zoom-level and the dimensions of the map canvas.

    You could also use longitude instead of latitude

提交回复
热议问题