When is Google updating Google Maps?

How can I center my google maps on my markers?

  • Here is my code for creating my map and trying to set it to center on all the markers: <div id="map" style="width: 500px; height: 400px;"></div> <script type="text/javascript"> var locations = [ ['Bondi Beach', -33.890542, 151.274856], ['Coogee Beach', -33.923036, 151.259052], ['Cronulla Beach', -34.028249, 151.157507], ['Manly Beach', -33.80010128657071, 151.28747820854187], ['Maroubra Beach', -33.950198, 151.259302], ['Annapolis', 38.956205,-76.492361] ]; var map = new google.maps.Map(document.getElementById('map'), { zoom: 10, center: new google.maps.LatLng(-33.92, 151.25), mapTypeId: google.maps.MapTypeId.ROADMAP }); var infowindow = new google.maps.InfoWindow(); var marker, i; for (i = 0; i < locations.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(locations[i][1], locations[i][2]), map: map }); google.maps.event.addListener(marker, 'click', (function(marker, i) { return function() { infowindow.setContent(locations[i][0]); infowindow.open(map, marker); } })(marker, i)); } var latlngbounds = new GLatLngBounds(); for (var i = 0; i < latlng.length; i++) { latlngbounds.extend(latlng[i]); } map.setCenter(latlngbounds.getCenter(), map.getBoundsZoomLevel(latlngbounds)); </script> I am getting this error in google chrome: Uncaught ReferenceError: GLatLngBounds is not defined I have no idea why this is not working....

  • Answer:

    Before looping through the locations array, create variable for the bounds. var bounds = new google.maps.LatLngBounds(); And while looping the array, add the position to the bounds array. bounds.extend(position); I would put that after the })(marker, i)); line, just before the closing bracket of the for-loop. After the looping is done, fit the map to the bounds. It might be a good idea to check if the bounds array actually contains anything, before fitting the map. Just in case. if(bounds.length > 0){     map.fitBounds(bounds); } This way you don't have to loop through the location again as you now do in the end of the script. Haven't tested this but I feel lucky :) I guess the error you get happens because you use Google Maps API 3 and GLatLngBounds is API 2 function.

Pekka Ruuska at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.