i have a google map with multiple markers and each has its own infowindow.
nothing happens when i click. fyi: i know it the listener fires because i did put a alert in ther before and it worked.
Problem Code is:
google.maps.event.addListener(point[i], 'click', function() {
infowindow[i] = new google.maps.InfoWindow({content: contentString[i] });
infowindow[i].open(map,point[i]);
});
if i only do
infowindow[i] = new google.maps.InfoWindow({content: contentString[i] });
infowindow[i].open(map,point[i]);
it works. but not in my addListener function. i guess ther is something that googlemaps doesnt like but firebug gives me 0 errors..
really need your help. Thanks a lot!
It is because you probably have closure within loop! So the variable
iin callback is already overwritten at the time when the callback is called. You have two options how to fix it:1) classical “closure in loop” workaround (you do another closure for every loop iteration):
2) avoid closure and use the marker data structure:
(or you could also move the contentString also to the marker:
point[i].contentString = ...and usethis.contentStringin the click handler. Then you don’t need thepoint[i].iattribute.)Personally I much more prefer the 2nd solution over the first, since the closures consume memory etc.