I\'m doing some testing and ran into this bizarre situation: The first case (assigning objects like InfoWindows in a loop) does not work as expected, while writing the assig
var iwArray = [];
for (var i = 0; i < 3; i++) {
iwArray[i] = new google.maps.InfoWindow({content: "w " + i });
google.maps.event.addListener(marker[i], 'mouseover', createListener(i, map));
}
function createListener(i, map) {
return function(e) {
iwArray[i].open(map, this);
};
}
Use closure:
for (var i = 0; i < 3; i++) {
(function(i){
iwArray[i] = new google.maps.InfoWindow({content: "w "});
google.maps.event.addListener(marker[i], 'mouseover', function(e) {
iwArray[i].open(map, this);
});
})(i);
}