3 回答

TA贡献1866条经验 获得超5个赞
如果标记位于同一建筑物中,则偏移标记不是真正的解决方案。您可能想要做的就是像这样修改markerclusterer.js:
在MarkerClusterer类中添加一个原型click方法,如下所示-我们稍后将在地图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()是您的函数(尚待编写),用于弹出一个信息窗口,其中包含可供选择的选项列表。请注意,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;
}
添加回答
举报