I've created a set of markers using an array within one variable. I'd like to add a "click" event listener to each marker that links to a url which is specified for each item in the array. The following function works perfectly EXCEPT that it assigns the last url in the array to all of the markers (instead of assigning the first url to the first marker and the second url to the second marker and so on). The rest of the data is assigned appropriately. I know that the solution involves a closure in the loop, but I can't figure out how to properly create it. Any help would be much appreciated.
var tours = [
['Title 1', 35.22024012078826, -80.81088066101074, 'tour/1'],
['Title 2', 35.20376057077245, -80.84718704223633, 'tour/2']
];
setTourMarkers(map, tours);
function setTourMarkers(map, locations) {
var tourImage = new google.maps.MarkerImage('http://www.tours.com/images/camera-icon.png',
new google.maps.Size(27, 22),
new google.maps.Point(0,0),
new google.maps.Point(14, 11));
var tourShape = {
coord: [1, 1, 1, 27, 27, 22, 22 , 1],
type: 'poly'
};
for (var i = 0; i
以上就是Attach event listeners to a set of markers using one variable in Google Maps v3 - loop, closure的详细内容,更多请关注web前端其它相关文章!