Does anyone have any suggestions on how to automatically close info windows when another marker is clicked? Currently the user has to manually close the info box when clicking another marker. I was hoping there was a method I could easily incorporate into my code. It looks like I'm creating separate infoboxes for each marker. When we go to scale this project up we are going to have 10k plus markers that will be separated by different categories.
I read about the suggestion of creating one infoWindow and then changing the contents, but if I understand my code correctly I'm creating individual infoboxes for each marker since they are coming out of the XML file. How could I incorporate this technique with my current code?
You can find a live example of my project here: http://www.officerage.com/maps/ Here is my example code:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<meta charset="UTF-8">
<title>A Basic Map</title>
<style>
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
function infoCallback(infowindow, marker) {
return function() { infowindow.open(map, marker); };
}
function initMap() {
var mapOptions = {
zoom: 3,
center: new google.maps.LatLng(37.5, -122),
mapTypeId: 'roadmap'
};
var map = new google.maps.Map(document.getElementById('map'), mapOptions);
$.ajax({
type: "GET",
async: true,
url: "testxml.xml",
dataType: "xml",
success:
function (xml) {
var places = xml.documentElement.getElementsByTagName("place");
for (var i = 0; i < places.length; i++) {
var lat = places[i].getAttribute('latitude');
var long = places[i].getAttribute('longitude');
var category = places[i].getAttribute('Info');
var latLng = new google.maps.LatLng(lat, long);
var marker = new google.maps.Marker({
position: latLng,
map: map,
category:places[i].info,
label:places[i].name
});
var infowindow = new google.maps.InfoWindow({
content:category});
google.maps.event.addListener(marker, 'click', infoCallback(infowindow,marker));
}
}
}
);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCz_-TAwsP-dL_dsEkmz8sGEU1hyPYKHik&callback=initMap">
</script>
</body>
</html>