for (var i=0; i<layerCount; i++){
for (var j=0; j<layerData[i].data.length; j++){
var text = layerData[i].data[j].text;
var latlng = new google.maps.LatLng(layerData[i].data[j].lat, layerData[i].data[j].lng);
var marker = new google.maps.Marker({map: map, position: latlng});
var infowindow = new google.maps.InfoWindow({content: text});
google.maps.event.addListener(marker, 'click', function() {infowindow.open(map,marker);});
}
}
The problem in the above code most likely is in the line starting with google.maps.event.addListener. Whichever marker I click, I get a infowindow opening up for the last item in the data field of layerdata[i].data[j]. Seems like I am calling by reference and not by value, so infowindow and marker for all listeners are the last items in the arrays.
But how can I solve this?
many thanks!
You have to create a closure:
See if it helps 😉