防止标记聚集在MarkerClusterer中

时间:2012-08-06 22:05:03

标签: javascript google-maps google-maps-api-3 markerclusterer

有没有办法阻止个别标记与MarkerClusterer聚类?

这是我的代码:

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

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

1 个答案:

答案 0 :(得分: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);
    }
});

OR 事件更好,让标记从头开始存储群集器:

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