Call map.fitBounds () several times in the Google Maps API v3.0

I just started using the Google Maps API (v3.0) and still have had great success. I load a set of objects with Latitude and Longitude values ​​from the database, passing them to my script and iterating over them in the script to add them to the map.

I use the bounds.extend() / map.fitBounds() "method to set the map scale and border (see code below), which works as expected for the first time; however, if I clear existing markers, I will take a different set of objects and do the same thing on one copy of the map, it incorrectly sets the boundaries, which usually leads to minimal scaling (view of an astronaut).

My suspicion is that my map object has a memory of the previous set of restrictions that I gave it, and that I need to find a way to clear these boundaries before assigning my new ones, but I really can't be too sure.

Any help is much appreciated!

 var locationList = []; for (var i = 0; i < mapPoints.length; i++) { // mapPoints is a collection of DTOs var mapPoint = mapPoints[i]; var location = new google.maps.LatLng(mapPoint.Latitude, mapPoint.Longitude); locationList.push(location); var marker = new google.maps.Marker({ map: map, icon: '/Content/images/map/' + mapPoint.Status.Icon, shadow: '/Content/images/map/shadow.png', position: location }); markers.push(marker); // markers is an Array that is managed outside this loop } var bounds = new google.maps.LatLngBounds(); for (var j = 0; j < locationList.length; j++) bounds.extend(locationList[j]); map.fitBounds(bounds); 
+6
google-maps
source share
2 answers

This is not an answer, so to speak, but a (slightly hacky) workaround that I found on thread in the Google Maps Javascript API v3 API:

 //map.fitBounds(bounds); setTimeout( function() { map.fitBounds( bounds ); }, 1 ); 
+12
source share

if the above answer does not work for you (this is not for me), the problem may be downloading (if you use it). bootstrap modals specifically generate all kinds of theft when I embed a map object in it. Curiously correcting myself if / when I throw a β€œwarning” there. In any case, I solved all my problems by simply creating my own modal (i.e. without using bootstrap modalities).

0
source share

All Articles