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

查看:19
本文介绍了同一地点有多个标记 - 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.

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天全站免登陆