-1

I try to add infowindows for all my markers. But I add only one InfoWindow for last marker. Can you help me?

for (i = 0; i < events.length; i++) {
    var contentString = '<div id="content">'+i+'bla-bla-bla</div>';
    var infowindow = new google.maps.InfoWindow({
       content: contentString
    });
    var newMarker = new google.maps.Marker({
       position: new google.maps.LatLng(events[i][1], events[i][2]),
       map: map,
       title: events[i][0]
     });
     newMarker.categorysassasas = 2;
     newMarker.category = events[i][3];
     markers.push(newMarker);
     google.maps.event.addListener(newMarker, 'click', function() {
        infowindow.open(map,newMarker);
     });
}
lfergon
  • 963
  • 15
  • 27
Nick
  • 1,530
  • 2
  • 15
  • 22
  • 4
    possible duplicate of [Multiple Google Maps infowindow](http://stackoverflow.com/questions/4381355/multiple-google-maps-infowindow) – Dr.Molle May 25 '12 at 05:51
  • Very weird, the code seems to be correct. I can't solve your problem, just give you a suggestion: remove all unuseful stuff and simplify the code (e.g. give contentString the value "hello") and remove the categories (newMarker.category) – Daniele B May 27 '12 at 16:15

1 Answers1

-1

found a similar solution to your question in this stackoverflow: Trying to bind multiple InfoWindows to multiple Markers on a Google Map and failing

Essentially all you'll have to do is wrap your addlistener function within another function to create a closure for your marker variable - so that it is bound to the infowindow. In your case, I believe you'll have to add the listenMarker function outside of your init() and pass in the variable for marker and infowindow, like so..

function listenMarker (marker, info)
{
  // so marker is associated with the closure created for the listenMarker function call
  google.maps.event.addListener(marker, 'click', function() {
    info.open(map, marker);
  });
}

and then in your for loop where you call the addlistener event, replace that with:

listenMarker(newMarker, infowindow);

That should do the trick.

Community
  • 1
  • 1
Suvi Vignarajah
  • 5,758
  • 27
  • 38
  • re-edited my entire answer instead of adding another one, let me know if this helps – Suvi Vignarajah May 27 '12 at 16:07
  • can you paste me your code and show me how you're doing it? you should also omit newMarker.categorysassasas = 2; from your code – Suvi Vignarajah May 31 '12 at 18:15
  • I found the example. http://www.geocodezip.com/v3_MW_example_map1.html . But if i change the script, for example - for (i = 0; i < 5; i++) { var point = new google.maps.LatLng(43.65654,-79.90138); var marker = createMarker(point,'
    Some stuff to display in the First Info Window. With a Link<\/a> to Mike Williams' home page<\/div>') } the script doesn't work.
    – Nick Jun 01 '12 at 15:49
  • are you also including the createMarker function in your script? you can just create markers using the `var marker = new google.maps.Marker({ position: point, title:"enter text" });` initialization. You can bind your info window to the marker in this for loop as well, by calling the listenMarker function i explained earlier. – Suvi Vignarajah Jun 01 '12 at 19:31
  • also, just thought I'd point out that your for loop will display 5 markers in the same position (because your point variable is not changing). – Suvi Vignarajah Jun 01 '12 at 19:32