I load an array of markers from a JSON string. The JSON string contains marker location and information that needs to be displayed on each marker's popup.
I have the following code
function AddAllMarkers(markersJSON){
var tempArray = JSON.parse(markersJSON);
infoWindow = new google.maps.InfoWindow();
for(var i = 0; i < tempArray.Locations.length; i++){
var obj = tempArray.Locations[i];
var point = new google.maps.LatLng(obj.Latitude, obj.Longitude);
var contentString = <!--CONTENT STRING GETS SET HERE PER MARKER-->;
var markerTemp = new google.maps.Marker({
position: point,
icon:obj.Icon
});
google.maps.event.addListener(markerTemp, 'click', function() {
infoWindow.close();
infoWindow = new google.maps.InfoWindow();
infoWindow.setContent(contentString);
infoWindow.open(map, this);
});
markerArray.push(markerTemp);
}
}
When this function gets called I load all the markers correctly but all markers show the popup of the last marker loaded. What am I doing wrong? I did try moving the declaration of infoWindow around but either that does not solve the problem or it causes multiple balloons to be opened at the same time.
How can I solve this?