![](/img/trans.png)
[英]How to return result of nested function in JavaScript when using addListener?
[英]How to add an addListener function for an object javascript?
我为标记添加了一个监听事件,例如
var markers = locations.map(function(location, i) {
return new google.maps.Marker({
position: location,
label: labels[i % labels.length]
});
});
// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(map, markers, {
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
markerCluster.addListener('click', function() {
console.log('click action');
});
当我单击Google地图上的标记时。 我的点击功能不起作用。 如何执行这项任务?
您可能需要一个自定义的Google点击处理程序,例如:
google.maps.event.addListener(markerCluster, 'clusterclick', function(cluster) {
// your code here.
});
您必须将侦听器添加到标记对象。
https://developers.google.com/maps/documentation/javascript/events
marker.addListener('click', function(){
// do something here.
})
由于您将其作为数组返回到函数中,因此将...
for(id in markers) {
markers[id].addListener('click',function(){
//your code});
}
那应该工作。
看看这个JSFiddle
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.