【问题标题】:Preventing markers from being clustered in MarkerClusterer防止标记在 MarkerClusterer 中聚集
【发布时间】:2012-08-03 22:02:08
【问题描述】:

有什么方法可以防止单个标记被 MarkerClusterer 聚类?

这是我的代码:

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢,这很有帮助。
    猜你喜欢
    • 2012-12-20
    • 2012-05-17
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 2012-03-22
    • 1970-01-01
    相关资源
    最近更新 更多