MarkerClusterer在同一个地方有多个标记 [英] More than one marker on same place - MarkerClusterer

查看:140
本文介绍了MarkerClusterer在同一个地方有多个标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using MarkerClusterer. When I have two or more markers on the exact same spot, The API only displays 1 marker - the top one. But somehow I want to show all the markers as each one will be opening distinct popup. I have searched found few solutions but none are seem to be working Anybody had similar issue and would pls share a solution??

推荐答案

终于搞定了。这是所有那些还没有找到解决方案的人。下面的代码将偏移量添加到同一位置上的标记中:

Finally got it working. This is for all those who still haven't found a solution. Below code adds offset to the markers on same location:

createMarker 函数中添加以下代码:

In your createMarker function add this code:

//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);
        }
    }
}

引用此

现在使用新的头寸值更新每个标记的 google.maps.Marker 对象 - finalLatLng $ b

Now update your google.maps.Marker object for each marker with new position value – 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);

这篇关于MarkerClusterer在同一个地方有多个标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆