I'm using the V3 version of google maps, and wondered how to go about this-- I have an array of google.maps.Markers with their LatLng positions set.
I would like to loop through the array and find the min/max latitude and longitude values of the markers, and center and resize the map so all the markers fit on the screen, with a small fudge factor.
Looping through the markers array I can handle, but I'm not sure of how go about centering and resizing the map to fit them all (with a small border region surrounding, so no markers are flush against an edge.)
I'm not sure if this helps or matters but the <div> the map lives in is 320x200 pixels.
2048 characters in URL is just under 100 GeoCode values. So, again no more than 100 markers.
The initialize() function creates a Google Map with a marker. The transition() and moveMarker() are used to move marker smoothly on click on the Google map.
You can create a function like
function setBounds() {
var bounds = new google.maps.LatLngBounds();
for (var i=0; i < markersArray.length; i++) {
bounds.extend(markersArray[i].getPosition());
}
map.fitBounds(bounds);
}
As simple as that!
See the google.maps.Map fitBounds method
Add all the positions of your markers to an empty google.maps.LatLngBounds object, then call fitBounds on it.
var bounds = new google.maps.LatLngBounds();
for (var i=0; i < yourArrayOfMarkers.length; i++) {
bounds.extend(yourArrayOfMarkers[i].getPosition();
}
yourMap.fitBounds(bounds);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With