Consider the following locations:
var locations =
[{ "id": 1, "ReferenceNumber": "52525252525", "Address" : "School" , "Latitude": "21.585486", "Longitude": "50.258745" },
{ "id": 2, "ReferenceNumber": "6262626262", "Address" : "University", "Latitude": "21.54484411", "Longitude": "50.14846648" },
{ "id": 3, "ReferenceNumber": "424242424", "Address": "PUMPING STATION ", "Latitude": "21.9856341", "Longitude": "61.2587466" }];
With the below code, clicking multiple Markers will open multiple Infowindows. How can I prevent this to happen and make sure the previous Infowindow is closed before a new one is open?
$.each(locations, function(i, item) {
var marker = new google.maps.Marker({
'position': new google.maps.LatLng(item.Latitude, item.Longitude),
'map': map,
'title': item.Latitude + "," + item.Longitude
});
marker.setIcon('http://maps.google.com/mapfiles/ms/icons/red-dot.png')
var infowindow = new google.maps.InfoWindow({
content: "<div class='infoDiv'><h3>Reference#: </h3> <h6>" + item.ReferenceNumber + "<h3>Location: </h3> <h6>" + item.Address + "</div></div>"
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
google.maps.event.addListener(map, "click", function(event) {
infowindow.close();
//autoCenter();
});
})
Any help would be highly appreciated.