You Are Here Home > Google Maps API V3; infowindow is only attached to the last...
DirectorySync is a directory synchronizing and backup utility providing automated, real-time syncing and scheduled, configurable backups at an affordable price.

Google Maps API V3; infowindow is only attached to the last marker problem

You can fix this issue like this:

for (var i = 0; i < addresses.length; i++) {
			(function () { /* ################################################## */
				var address = addresses[i];
				var lat_lang = new google.maps.LatLng(address.latitude, address.longitude);
				var marker = new google.maps.Marker({
					position: lat_lang,
					map: map
				});
				markers[markers.length] = marker;
				var infowindow = new google.maps.InfoWindow({
					content: address.address
				});
				infowindows[infowindows.length] = infowindow;
				google.maps.event.addListener(marker, 'click', function() {
					close_infowindows();
					infowindow.open(map, marker);
				});
			})(); /* ################################################## */
		}

By adding the lines marked with /* ################################################## */ in the middle of your loop…

I hope this helps someone :)

Google Maps API V3; infowindow is only attached to the last marker problem
Filed under: APIs,Google Maps API,JavaScript,Web Development   Posted by: Codehead
Do you have any questions? ask here.




13 Comments »

  1. Alex:
     

    Thanks!!! :-)

    Comment

  2. Giulio:
     

    Yeah!!

    Thanks a lot!!!!
    :D

    Comment

  3. J... C... IN A HANDBASKET:
     

    HOW DOES IT WORK!??!

    THANKS!

    Comment

  4. Jan:
     

    It helped me a lot, but I have no idea, why it works in this way.

    Comment

  5. Qaiser:
     

    Dear, Thank you very much. It really helped me. God bless you….

    Comment

  6.  

    OMG! It work! Thank you!!!

    p.s. How does it work???

    Comment

  7.  

    Perfect!

    Comment

  8.  

    Thanks. It is that I need.

    Comment

  9. Bonecollector:
     

    It did help me A LOT. (more than my TAs @ Harvard)
    Thanks.

    Comment

  10. mcpiper:
     

    You are a genius and my friend. I too would love to know why this works

    Comment

  11. Codehead:
     

    You can get some ideas about how this works from this post:

    http://stackoverflow.com/questions/2421911/what-is-the-purpose-of-wrapping-whole-javascript-files-in-anonymous-functions-lik/2421949#2421949

    Comment

  12. Beum89:
     

    thanks a lot! that’s great!

    Comment

  13. Daniel:
     

    THANKS THANKS AND THANKS!!! YOU’RE THE BEST! YOU DON’T HAVE A IDEA HOW LONG I WAS SEARCHING FOR A SOLUTION OF MY PROBLEM!!!

    Greatings from Germany

    Comment

RSS feed for comments on this post. TrackBack URL

Leave a comment