1

有什么办法可以防止用MarkerClusterer聚集单个标记吗?防止标记在MarkerClusterer中聚簇

这里是我的代码:

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

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

回答

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

谢谢你,这是非常有帮助的。 – jeff 2012-08-07 17:48:05