如果标记位于同一建筑物中,则抵消标记不是真正的解决方案。您可能要做的是修改标记群集.js如下所示:
-
在 MarkerClusterer 类中添加一个原型单击方法,如下所示 - 我们稍后将在 map initialize() 函数中重写此方法:
MarkerClusterer.prototype.onClick = function() {
return true;
};
-
在 ClusterIcon 类中,在 clusterclick 触发器之后添加以下代码:
// Trigger the clusterclick event.
google.maps.event.trigger(markerClusterer, 'clusterclick', this.cluster_);
var zoom = this.map_.getZoom();
var maxZoom = markerClusterer.getMaxZoom();
// if we have reached the maxZoom and there is more than 1 marker in this cluster
// use our onClick method to popup a list of options
if (zoom >= maxZoom && this.cluster_.markers_.length > 1) {
return markerClusterer.onClickZoom(this);
}
-
然后,在 initialize() 函数中初始化映射并声明 MarkerClusterer 对象:
markerCluster = new MarkerClusterer(map, markers);
// onClickZoom OVERRIDE
markerCluster.onClickZoom = function() { return multiChoice(markerCluster); }
其中 multiChoice() 是 YOUR(尚未编写)函数,用于弹出一个信息窗口,其中包含可供选择的选项列表。请注意,markerClusterer 对象将传递给函数,因为您将需要它来确定该群集中有多少个标记。例如:
function multiChoice(mc) {
var cluster = mc.clusters_;
// if more than 1 point shares the same lat/long
// the size of the cluster array will be 1 AND
// the number of markers in the cluster will be > 1
// REMEMBER: maxZoom was already reached and we can't zoom in anymore
if (cluster.length == 1 && cluster[0].markers_.length > 1)
{
var markers = cluster[0].markers_;
for (var i=0; i < markers.length; i++)
{
// you'll probably want to generate your list of options here...
}
return false;
}
return true;
}