Google maps and using fitBounds with an XML list of markers
I am using the following to generate a map of markers. The XML file is generated using PHP based on addresses in a mysql database.
function initMap() { var map = new google.maps.Map(document.getElementById('map'), { center: new google.maps.LatLng(-26.863276, 135.207977), zoom: 4.8 }); var infoWindow = new google.maps.InfoWindow; // Change this depending on the name of your PHP or XML file downloadUrl('PATH_TO_map_xml.php', function(data) { var xml = data.responseXML; var markers = xml.documentElement.getElementsByTagName('marker'); Array.prototype.forEach.call(markers, function(markerElem) { var id = markerElem.getAttribute('id'); var name = markerElem.getAttribute('name'); var address = markerElem.getAttribute('address'); var type = markerElem.getAttribute('type'); var point = new google.maps.LatLng( parseFloat(markerElem.getAttribute('lat')), parseFloat(markerElem.getAttribute('lng'))); var infowincontent = document.createElement('div'); var strong = document.createElement('strong'); strong.textContent = name infowincontent.appendChild(strong); infowincontent.appendChild(document.createElement('br')); var text = document.createElement('text'); text.textContent = address infowincontent.appendChild(text); var icon = customLabel[type] || {}; var marker = new google.maps.Marker({ map: map, position: point, label: icon.label }); marker.addListener('click', function() { infoWindow.setContent(infowincontent); infoWindow.open(map, marker); }); }); }); }
Sometimes there are just 3 markers around one city, other times, there are 50 over a wider area.
The question is, can I use fitBounds to zoom the map to the extent of the markers?
Thanks!