2013-12-10 42 views
11

我正在使用MarkerClusterer。当我在同一个点上有两个或多个标记时,API只显示1个标记 - 最上面的一个标记。但不知何故,我想显示所有的标记,因为每个将打开不同的弹出窗口。 我已经搜索发现几个解决方案,但似乎没有工作 任何人都有类似的问题,并会共享一个解决方案?同一地点有多个标记 - MarkerClusterer

+2

的[集成Spiderfier JS成markerClusterer V3爆炸多标志物完全相同的经度/纬度]可能重复(http://stackoverflow.com/questions/9726920/integrating-spiderfier-js-into-markerclusterer -v3-explode-multi-markers-with) – geocodezip

+1

@geocodezip有没有一种方法可以在不使用OverlappingMarkerSpiderfier的情况下工作,因为它不能满足需求。只用很小的空间显示两个标记就可以知道有两个或更多的标记。谢谢。 – Grish

+0

当然有。只需编码就可以做到这一点。或者改变输入数据,以避免重复。 – geocodezip

回答

23

终于搞定了。这是所有那些还没有找到解决方案的人。下面的代码将抵消对同一位置的标记:

在你createMarker函数添加以下代码:

//get array of markers currently in cluster 
var allMarkers = namespace.mapParams.mapMarkersArray; 

//final position for marker, could be updated if another marker already exists in same position 
var finalLatLng = latlng; 

//check to see if any of the existing markers match the latlng of the new marker 
if (allMarkers.length != 0) { 
    for (i=0; i < allMarkers.length; i++) { 
     var existingMarker = allMarkers[i]; 
     var pos = existingMarker.getPosition(); 

     //if a marker already exists in the same position as this marker 
     if (latlng.equals(pos)) { 
      //update the position of the coincident marker by applying a small multipler to its coordinates 
      var newLat = latlng.lat() + (Math.random() -.5)/1500;// * (Math.random() * (max - min) + min); 
      var newLng = latlng.lng() + (Math.random() -.5)/1500;// * (Math.random() * (max - min) + min); 
      finalLatLng = new google.maps.LatLng(newLat,newLng); 
     } 
    } 
} 

Refer this

现在更新与新的位置值每个标记您的google.maps.Marker对象 - finalLatLng

var marker = new google.maps.Marker({ 
    map: msf_namespace.mapParams.resultmap, 
    position: finalLatLng, 
    title: name, 
    icon: markericon 
}); 

//add each generated marker to mapMarkersArray 
namespace.mapParams.mapMarkersArray.push(marker); 
+2

我更喜欢你的方法而不是蜘蛛技巧,但是我想知道你正在使用多少标记,并且你是否注意到了放置的放慢速度? – ow3n

+0

目前约有百位,至今没有性能问题 – Grish

+0

其作品适合我。 –

相关问题