看起来没有任何内置功能,但您可以做一些事情来简化操作。
我建议将集群存储在标记上以便于切换:
myClusterer = new MarkerClusterer(map, markers);
...
google.maps.event.addListener(marker, 'click', function() {
// if marker is detached from clusterer
if(marker.clusterer) {
clusterer.attachMarkers([marker]);
marker.clusterer = null;
// if marker is attached to clusterer
} else {
marker.clusterer = myClusterer;
clusterer.removeMarker(marker);
}
});
或者事件更好,让标记从一开始就存储集群:
myClusterer = new MarkerClusterer(map)
marker = new MyClusterableMarker();
marker.attachToClusterer(myClusterer)
...
google.maps.event.addListener(marker, 'click', function() {
marker.toggleAttachmentToClusterer();
});
...
$.extend(MyClusterableMarker.prototype, google.maps.Marker.prototype, {
attachToClusterer: function(clusterer) {
this.clusterer = clusterer;
this.clusterer.attachMarkers([this]);
this.attachedToClusterer = true;
},
toggleAttachmentToClusterer: function() {
if(this.attachedToClusterer) {
this.clusterer.removeMarker(this);
this.attachedToClusterer = false;
} else {
this.clusterer.addMarkers([this]);
this.attachedToClusterer = true;
}
}
})