title: Attach event listeners to a set of markers using one variable in Google Maps v3 - loop, closure
body: <p>I've created a set of markers using an array within one variable. I'd like to add a "click" event listener to each marker that links to a url which is specified for each item in the array. The following function works perfectly EXCEPT that it assigns the last url in the array to all of the markers (instead of assigning the first url to the first marker and the second url to the second marker and so on). The rest of the data is assigned appropriately. I know that the solution involves a closure in the loop, but I can't figure out how to properly create it. Any help would be much appreciated.</p>
  
  <pre><code>var tours = [
  
  ['Title 1', 35.22024012078826, -80.81088066101074, 'tour/1'],
  ['Title 2', 35.20376057077245, -80.84718704223633, 'tour/2']
  
  ];
  
  
  setTourMarkers(map, tours);
  
  
  function setTourMarkers(map, locations) {
  
  
  var tourImage = new google.maps.MarkerImage('http://www.tours.com/images/camera-icon.png',
    new google.maps.Size(27, 22),
    new google.maps.Point(0,0),
    new google.maps.Point(14, 11));
  
  
  var tourShape = {
    coord: [1, 1, 1, 27, 27, 22, 22 , 1],
    type: 'poly'
  };
  
  
  for (var i = 0; i &lt; locations.length; i++) {
    var tour = locations[i];
    var tourLatLng = new google.maps.LatLng(tour[1], tour[2]);
    var marker = new google.maps.Marker({
    position: tourLatLng,
    map: map,
    icon: tourImage,
    shape: tourShape,
    title: tour[0]
  
    }); //End new google.maps.Marker
  
    google.maps.event.addListener(marker, 'click', function() {
      window.location.href = "http://www.tours.com/" + tour[3];
  
    }); //End google.maps.event.addListener
  
  }; // End "for" statement ***************
  
  
  }; // End setTourMarkers function
  </code></pre>