Looping through Markers with Google Maps API v3 Problem
- by Oscar Godson
I'm not sure why this isn't working. I don't have any errors, but what happens is, no matter what marker I click on, it clicks the 3rd one (which is the last one out of 4 markers. Array starts at 0, obviously) and shows the number "3", which is correct for THAT one, but I'm not clicking that one. Here is most of my code, just not the array of [place-name, coordinates] (var locations, which you will see):
function initialize() {
var latlng = new google.maps.LatLng(45.522015,-122.683811);
var settings = {
zoom: 15,
center: latlng,
disableDefaultUI:true,
mapTypeId: google.maps.MapTypeId.SATELLITE
};
var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
var infowindow = new Array();
var marker = new Array();
for(x in locations){
console.log(x);
infowindow[x] = new google.maps.InfoWindow({content: x});
marker[x] = new google.maps.Marker({title:locations[x][0],map:map,position:locations[x][1]});
google.maps.event.addListener(marker[x], 'click', function() {infowindow[x].open(map,marker[x]);});
}
}
initialize()
The console.log output is (its correct, and what i expect):
0
1
2
3
So, any ideas?