title: Adding multiple markers with infowindows (Google Maps API)
body: <p>I'm currently using the following code the place multiple markers on a Google Map using their API.</p>
  
  <p>The problem I'm having is with multiple infowindows not working (only showing the last one).</p>
  
  <p>There are loads of questions like mine here on SO. Actually make that a shitload of questions :-)</p>
  
  <p>Just an example: <a href="https://stackoverflow.com/questions/2357323/trying-to-bind-multiple-infowindows-to-multiple-markers-on-a-google-map-and-faili">Trying to bind multiple InfoWindows to multiple Markers on a Google Map and failing</a></p>
  
  <p>The solution to my problem is kinda easy: just enclose the click listener to a (anonymous) function.</p>
  
  <p>However what I don't understand is why my solution isn't working (saving the markers and infowindows in arrays instead of just one variable).</p>
  
  <pre><code>    var markers = [];
      var infowindows = [];
  
      // add shops or malls
      for (var key in data.markers) {
        if (data.markers.hasOwnProperty(key)) {
          infowindows[key] = new google.maps.InfoWindow({
              content: data.markers[key].infowindow
          });
  
          markers[key] = new google.maps.Marker({
                  position: new google.maps.LatLng(data.markers[key].location.lat, data.markers[key].location.lng),
                  map: map,
                  flat: true,
                  title: data.markers[key].name,
                  draggable: false
          });
          var iconFile = 'http://maps.google.com/mapfiles/ms/icons/'+marker_color+'-dot.png';
          markers[key].setIcon(iconFile);
  
          google.maps.event.addListener(markers[key], 'click', function() {
            infowindows[key].open(map, markers[key]);
          });
        }
      }
  </code></pre>
  
  <p>So... I don't what to get the solution how to get it working with some function to enclose the listener (although it should work, haven't tested it yet but will), but I want to know the reason why it wouldn't work if I add the markers and infowindows to arrays instead.</p>