亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Google Maps API V3-多個標記完全相同

Google Maps API V3-多個標記完全相同

慕妹3146593 2019-11-22 14:34:55
有點卡在這一個。我正在通過JSON檢索地理坐標列表,并將其彈出到Google地圖上。除了在我完全相同的位置上有兩個或多個標記的情況下,其他所有程序都運行良好。該API僅顯示1個標記-最上面的一個。我想這很公平,但我想找到一種以某種方式顯示它們的方法。我已經搜索過google并找到了一些解決方案,但它們似乎主要針對的是V2的API或效果不佳。理想情況下,我想要一個解決方案,其中您單擊某種組標記,然后顯示聚集在所有標記周圍的標記。任何人都有這個問題或類似問題,是否愿意分享解決方案?
查看完整描述

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;

}


查看完整回答
反對 回復 2019-11-22
  • 3 回答
  • 0 關注
  • 368 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號