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);
google-maps
egoodberry
source share