I have a list of Google Maps API markers in a dictionary called markerList. All of them are being placed on the map. Then I create an array of all my infowindow's. Then I need to addListener for each of them so that when the marker is clicked on the map, it will call infowindow.open on the correct map and infowindow.
Unfortunately, when I click on a marker, I get the following error in firebug console:
infowindow is not defined
infowindow.open(map,infowindowList[window]);
Here is my code:
var infowindowList = {};
for (var aMarker in markerList){
infowindowList[aMarker] = new google.maps.InfoWindow({
content: "This is a test!",
size: new google.maps.Size(50,50)
});
console.log(infowindowList[aMarker]);
}
console.log("Done creating infowindowList...\n...Adding listeners");
for (var window in infowindowList){
google.maps.event.addListener(markerList[window], 'click', function() {
// infowindow.open(map,infowindowList[window]); // wrong, see Edit
infowindowList[window].open(map,markerList[window]);
});
console.log("added listener for window: ", infowindowList[window]);
}
I think this is happening because in the google.maps.event.addListener(markerList[window]... function, I register the function to be run on click as infowindow.open(map,infowindowList[window]), but that value, window, is local to the function, and so when I click on the marker, that function doesn't work, because it doesn't know what window is. Is that correct?
EDIT: As geocodezip pointed out, infowindow does not exist. So I changed it to infowindowList[window].open(map,markerList[window]);, also changing the last argument to markerlist[window] because it should be a marker as an arg, not another infowindow.