I have had and resolved your exact problem. The issue is that in Javascript for loops, the real variables are not used, but rather only copies of them. So, if you want to change the value of an external or passed variable, you have to make a function call OUTSIDE the loop to do the job. Here's my code:
Code:
function setMarkers() {
// Add markers to the map
for (var i = 0; i < _offices.length; i++) {
var office = _offices[i];
var myLatLng = new google.maps.LatLng(office[1], office[2]);
var contentString =
'<div class="infoWindow">'+
'<h3 class="heading">'+office[0]+'</h3>'+
'<p>'+office[4]+'</p>'+
'</div>';
var marker = new google.maps.Marker({
position: myLatLng,
map: _map,
title: office[0],
zIndex: office[3],
clickable: true
});
addInfoWindow(marker, contentString);
_markers.push(marker);
}
}
function addInfoWindow(marker, message) {
var info = message;
var infoWindow = new google.maps.InfoWindow({
content: message,
size: new google.maps.Size(200,50)
});
_infoWindows.push(infoWindow);
google.maps.event.addListener(marker, 'click', function () {
closeAllMarkers();
infoWindow.open(_map, this);
});
}
Note the call to addInfoWindow inside the for loop. Believe it or not, putting the code in the called function INSIDE the for loop will not work. Only the last item referenced, according to the length of the variable "offices" will be left to use. Putting the code for addInfoWindow OUTSIDE the for loop will produce the results you want. I'm definitely not an expert on this, but I have battered my brains out over it and appear to have won.