Map fit bounds for markers

1,874 views
Skip to first unread message

CSharp

unread,
Nov 15, 2010, 3:58:33 PM11/15/10
to google-map...@googlegroups.com
I currently set fitBounds for sets of markers so that the map can pan and zoom in or out to fit the set of markers on the map.  But in doing that, when the markers are de-selected from the list and the map is zoomed out based on the fitBounds, I noticed that the fitBounds does not zoom back in on one route that is re-selected and shown again.  It seems that the fitBound on a map occurs as a one time event to try and fit all markers on a map, but not repeated again on re-showing of markers.  Is there a way to make the fitBounds for a map to re-zoom in after the map has zoomed out or make the fitBounds zoom in and out according to the sets of markers that are displayed?
 
 
 

Esa

unread,
Nov 15, 2010, 4:52:05 PM11/15/10
to Google Maps JavaScript API v3
Each time you show a set of markers, you extend the bounds

this.m_mapBounds.extend(markers[i].getPosition());

but it is never resetted like

this.m_mapBounds = new google.maps.LatLngBounds();


geoco...@gmail.com

unread,
Nov 15, 2010, 6:55:22 PM11/15/10
to Google Maps JavaScript API v3
I would suggest creating the bounds for each object when it is first
processed, saving that bounds, then doing a union on the bounds of the
objects that are currently visible, using that resulting bounds as the
argument to fitBounds.

-- Larry

dirtHammer

unread,
Apr 24, 2012, 12:52:02 PM4/24/12
to google-map...@googlegroups.com
You can reset m_mapBounds (by setting LatLngBounds to null) before each new call for markers is made:

m_mapBounds = new google.maps.LatLngBounds(null);
Reply all
Reply to author
Forward
0 new messages