title: Add event listeners to several map markers
body: <p>I have the following function to set the Markers.</p>
  
  <pre><code>function setMarkers(map, locations, contenido) {
        // Add markers to the map
        console.log(contenido);
        // Marker sizes are expressed as a Size of X,Y
        // where the origin of the image (0,0) is located
        // in the top left of the image.
  
        // Origins, anchor positions and coordinates of the marker
        // increase in the X direction to the right and in
        // the Y direction down.
        var image = {
          url: '&lt;?php echo Yii::$app-&gt;request-&gt;baseUrl."/images/pinrojo.png";?&gt;',
          // This marker is 20 pixels wide by 32 pixels tall.
          size: new google.maps.Size(20, 32),
          // The origin for this image is 0,0.
          origin: new google.maps.Point(0,0),
          // The anchor for this image is the base of the flagpole at 0,32.
          anchor: new google.maps.Point(0, 32)
        };
        // Shapes define the clickable region of the icon.
        // The type defines an HTML &amp;lt;area&amp;gt; element 'poly' which
        // traces out a polygon as a series of X,Y points. The final
        // coordinate closes the poly by connecting to the first
        // coordinate.
        var shape = {
            coords: [1, 1, 1, 20, 18, 20, 18 , 1],
            type: 'poly'
        };
  
        var objeto_contentString = {};
        var objeto_infowindow = {};
        var object_marker = {};
  
        for (var i = 0; i &lt; locations.length; i++) {
              objeto_contentString['contentString' + i ] = contenido[i]["META"] + "&lt;BR/&gt;" + contenido[i]["VENTA"];
              objeto_infowindow['infowindow' + i] = new google.maps.InfoWindow({
              content: objeto_contentString['contentString' + i ]
          });
          var beach = locations[i];
          var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
              object_marker['marker' + i] = new google.maps.Marker({
              position: myLatLng,
              map: map,
              icon: image,
              shape: shape,
              title: beach[0],
              zIndex: beach[3]
          });
  
  
            google.maps.event.addListener(object_marker['marker' + i], 'click', function() {
            objeto_infowindow['infowindow' + i].open(map,object_marker['marker' + i]);
          });
        }
  
         console.log(objeto_contentString);
      }
  </code></pre>
  
  <p>I would like to know how to add an event listener to every marker, but how? Is there a way to dynamically add those element listeners? I heard about some closure, but not sure how to implement them. With the current code, it will add only a listener to the last element of the array in the loop.</p>