我有一个包含地图所有标记的数组。即使将 MarkerClusterer 与它们一起使用,我也可以毫不费力地将它们放在地图上。MarkerClusterer 的 addListener 工作得很好,但我只是无法让标记 addListener 触发事件。这是实际的代码:
function initializeMap(markerArray) {
var latlng = new google.maps.LatLng(0.0, 0.0);
var mapOptions = {
zoom: 2,
maxZoom: 21,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var length = markerArray.length,
element = null;
for (var i = 0; i < length; i = i + 2) {
var latlng = new google.maps.LatLng(markerArray[i],markerArray[i+1]);
var marker = new google.maps.Marker({
map: map,
position: latlng,
title:'Click to zoom'
});
markers.push(marker);
}
google.maps.event.addListener(marker, 'onclick', function() {
alert("I have been clicked");
});
var markerCluster = new MarkerClusterer(map, markers);
google.maps.event.addListener(markerCluster, 'clusterclick', function(cluster) {
alert("I have been clicked");
});
}