起初,我创建了一个for循环,该循环创建了我的标记(来自存储在数组中的数据),因为不可能延迟,所以我创建了setTimout(function()。
此循环函数循环,直到var counter达到var maxLoops。问题是,我找不到从这个重新创建的循环之外到达这些创建的标记的方法。
我想检索被单击的特定标记的"customProperty"的内容。
google.maps.event.addListenerOnce(map, 'idle', function() {
(function next() {
if (counter === maxLoops) {
markersGeladen = 1;
};
if (counter++ >= maxLoops) {
return;
}
setTimeout(function() {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[counter - 1][1], locations[counter - 1][2]),
map: map,
title: locations[counter - 1][0],
icon: icon,
customProperty: dataLikeText //<-- as mentioned before
});
markerArray.push(marker);
if (!audio.ended && counter > 2) {
audio2.play();
}
if (!audio.ended && !audio2.ended && counter > 2) {
audio3.play();
} else {
audio.play();
}
speed = speed * 0.89;
next();
}, speed);
})();
});
var markerArray = [];
var marker;
var map;
var markersGeladen = 0;
var locations = [
['FabLab Maastricht', 50.851368, 5.690973], //more items are placed
['Enschede', 52.22627199999999, 6.922312099999999]
];
var zoom;
if (window.innerHeight < 750) {
zoom = 7;
} else {
zoom = 8;
} //adjusts zoom of g-maps
var style = []; //style here
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 52.113252,
lng: 5.542603
},
zoom: zoom,
disableDefaultUI: true,
scrollwheel: false,
disableDoubleClickZoom: true,
navigationControl: false,
mapTypeControl: false,
scaleControl: false,
draggable: false,
styles: style
});
var maxLoops = 26;
var counter = 0;
var speed = 800;
var icon = {
url: "img/marker.png",
scaledSize: new google.maps.Size(75, 75)
};
var audio = new Audio('audio/plop.mp3');
var audio2 = new Audio('audio/plop.mp3');
var audio3 = new Audio('audio/plop.mp3');
google.maps.event.addListenerOnce(map, 'idle', function() {
(function next() {
if (counter === maxLoops) {
markersGeladen = 1;
};
if (counter++ >= maxLoops) {
return;
}
setTimeout(function() {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[counter - 1][1], locations[counter - 1][2]),
map: map,
title: locations[counter - 1][0],
icon: icon,
customProperty: dataLikeText //<-- as mentioned before
});
markerArray.push(marker);
if (!audio.ended && counter > 2) {
audio2.play();
}
if (!audio.ended && !audio2.ended && counter > 2) {
audio3.play();
} else {
audio.play();
}
speed = speed * 0.89;
next();
}, speed);
})();
});
}
产品最终结果需要超时来创建动画,因此我无法返回for循环
相关问题:如何使用带有Google Map API v3 的event.addListener获取已触发的标记
点击监听器函数中的this是对标记的引用
访问其customProperty
如下:
this.customProperty