title: Setting infoWindow for a list of markers created inside a callback function (directionsService)
body: <p>I'm trying to set infoWindows for markers that are created in the callback function of a directionsService (Google Maps API V3). I tried many different ways to do that without any luck..</p>
  
  <p>Here is a skeleton of how my code looks like:</p>
  
  <pre><code>for(i=0;i&lt;markersList.length;i++){
  map = someMap
  var request = myRequestObject;
  var directionsService = new google.maps.DirectionsService();
  var directionsDisplay = new google.maps.DirectionsRenderer();
  directionsDisplay.setMap(map);
  
  directionsService.route(request, (function (address) {
    return function(response, status) {
      //Use the directions service to do some stuff
      //..Then create markers
       var marker = new google.maps.Marker({
                         map: map,
                         title: address['title']
                     });
      //push the marker to a list of markers attached to the map
      //this can be useful to delete all markers in a later step
      map.markers.push(marker);
  
      //Creating multiple instances of infoWindows works fine though..
      //However, Creating 1 info window for the whole map doesn't work from here
      //If I try that, I'd set the same content for all info windows
      //which is not what I want.
    }
   }
  )(markersList[i])
  );
  
  }
  </code></pre>
  
  <p>I tried doing this, which didn't work either:</p>
  
  <pre><code>//add setInfo as a custom function to my map
  //then call it to iterate over all markers and add info windows
  google.maps.Map.prototype.setInfo = function() {
  
     for(var i=0; i &lt; this.markers.length; i++){
          infowindow.setContent(this.markers[i].getTitle());
          google.maps.event.addListener(this.markers[i], 'click', function(){
              infowindow.close();
              infowindow.open(map,this.markers[i]);
              });
      }
  };
  </code></pre>
  
  <p>Then I'd call this function after I'm done with directionsService.route (check the first code block), outside my main for-loop. However, it never finds any markers attached to the map for some reason..</p>
  
  <p>Any thoughts on how to assign the right infoWindows to the map markers?
  I want to have 1 infoWindow instance so that I could close it when a new infoWindow is clicked (infoWindow.close()).</p>
  
  <p>Thanks!</p>