Google Maps API V3 fitbounds() zooms out but never in

前端 未结 12 2421
感动是毒
感动是毒 2020-12-19 03:44

I\'ve created a quite complex store locator of sorts. The user enters their zip and a table returns results with corresponding markers on a map. They can page through result

12条回答
  •  青春惊慌失措
    2020-12-19 03:50

    It turns out that when you call map.getBounds() it returns the viewport with a little margin around the edges. Since this new bounds is larger than the current bounds, the map will always zoom out. I was able to solve it by avoiding using the current map's bounds altogether and maintaining a separate LatLngBounds variable. Every time I added a point I called:

    markersBounds.extend(newMarkersLatLng);
    map.fitBounds(markersBounds);
    map.panToBounds(markersBounds);
    

    To remove points (I was always adding them), you could make a new LatLngBounds object with the first point, then extend each remaining point to get your new bounds:

    var markersBounds = new google.maps.LatLngBounds(markers[0].getPosition(),
                                                     markers[0].getPosition());
    for(var i=1; i

提交回复
热议问题