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;
}
添加回答
舉報