如何为对象javascript添加addListener函数?

时间:2016-12-21 10:07:22

标签: javascript google-maps

我为标记添加了一个监听事件,例如

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');
});

当我点击谷歌地图上的标记时。我的点击功能不起作用。如何实现这个任务?

2 个答案:

答案 0 :(得分:1)

您可能需要一个自定义Google点击处理程序,如:

google.maps.event.addListener(markerCluster, 'clusterclick', function(cluster) {
  // your code here.
});

答案 1 :(得分:1)

您必须将侦听器添加到标记对象。

bulk IP address

marker.addListener('click', function(){
    // do something here.
})

因为你将它作为一个数组返回到你的函数中,它将是......

for(id in markers) { 
    markers[id].addListener('click',function(){
        //your code});     
}

这应该有效。

查看此https://developers.google.com/maps/documentation/javascript/events