I'm trying to iterate through an array, like so:
var locs = [
['Location 0', 50, 91],
['Location 1', 50, 100]
];
for (i = 0; i < locs.length; i++) {
$('#' + i).click(function(i) {
map.panTo(new google.maps.LatLng(locs[i][1], locs[i][2]));
})
}
But I get the unexpected identifier because of the way loops work in Javascript. I need it to iterate through the locs
array.
How do I do this? So frustrated!
I tried the return function
thing but it didn't work.
E.G.:
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
$('#' + i).click(function(marker, i) {
return function() {
map.panTo(new google.maps.LatLng(locations[i][1], locations[i][2]));
}
)};
}