Possible Duplicate:
Center map and zoom to fit the markers on the screen
I’m just writing an app that uses Google Maps. The app adds multiple points to a map. I would like to be able to focus the map on these multiple points after they’ve been added, but don’t know how.
So far, my inclination is to do the following:
- When adding points, keep track of min longitude, max longitude, min latitude, max latitude
- Calculate avg longitude, avg latitude
So.. Now I would know how to find my map center.
Does anyone know how I would calculate the zoom?
Add the locations to an empty google.maps.LatLngBounds object.
Call google.maps.Map.fitBounds on the resulting bounds.
psuedo code (not tested, assumes your markers are in an array in scope named gmarkers, assumes your google.maps.Map object is called “map”):