同一地点有多个标记

编程入门 行业动态 更新时间:2024-10-10 14:24:35
本文介绍了同一地点有多个标记 - MarkerClusterer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用 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屋!

更多推荐

[db:关键词]

本文发布于:2023-03-31 21:58:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/815350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   标记   地点

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!