1

有什么方法可以防止单个标记与 MarkerClusterer 聚集在一起?

这是我的代码:

google.maps.event.addListener(marker, 'click', function() {
    // prevent marker from being clustered here
});

我知道我可以从 MarkerClusterer 对象中删除标记,然后再将其添加回来,但这会有点复杂,我想知道是否有任何内置功能可以做到这一点。我浏览文档结果是徒劳的,但我可能会遗漏一些东西。

4

1 回答 1

3

看起来没有任何内置功能,但您可以做一些事情来简化操作。

我建议将集群存储在标记上以便于切换:

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;
        }
    }
})
于 2012-08-06T23:14:16.460 回答