1

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.

kalaracey
  • 815
  • 1
  • 12
  • 28

1 Answers1

2

You are getting the error "infowindow is not defined" because the infowindow variable is not defined. It certainly isn't defined in the code you posted.

Usually it is defined globally so there is one infowindow (the v2 default behavior), but in that case you still have to define it and create it (usually in the onload function).

working example

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • Ok, thank you, so I fixed that line to be `infowindowList[window].open(map,markerList[window]);`, but now, regardless of which marker I click on, it will always display the message above one marker – kalaracey Jul 02 '12 at 23:00
  • @kalaracey The listeners all reuse the same `window` variable that has completed the loop, that's why it's popping open over the last marker. See the `addListener` in [**this answer**](http://stackoverflow.com/a/3059129/1325290) The function wrapper creates a new scope for `window` and saves that value for each marker (replace `marker, i` with `window`). The way your code is written, it will end up opening several InfoWindows, most likely you only want one, then follow the method in the answer I linked. – Tina CG Hoehr Jul 02 '12 at 23:19
  • That is because after the loop is done, window always has the same value. That can be fixed with a function closure (if you look at the example the createMarker function holds the infowindow content associated with the marker in a function closure). – geocodezip Jul 02 '12 at 23:23