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
I'm a programmer at 