单击标记会打开错误的弹出窗口

Wrong pop up gets opened on marker click

我有很多标记,所以我将它们聚类。我遇到了问题,当用户从集群中缩小时我想保持弹出窗口打开,我找到了这个解决方案

https://jsfiddle.net/sghL4z96/65/

Leaflet Markercluster: Exempt marker from clustering

它工作正常。但问题是当标记离集群本身太近时 当我尝试使用相同的解决方案时,我得到了这个结果

https://jsfiddle.net/s2mnvL5w/3/

当我点击集群时,两个标记显示 up.For 例如,如果我点击左边的那个,我会弹出文本 one.When 我关闭这个弹出窗口,我再次尝试打开左边的标记然后我弹出文本二是 wrong.Instead 我得到 one.Where 是我的错误并且 hopw 解决方案可以调整到坐标非常接近的标记。

clustered.on('popupopen', function(e) {
    console.log('open');
    const m = e.popup._source;
    clustered.removeLayer(m);
    unclustered.addLayer(m);
    m.openPopup();
});
unclustered.on('popupclose', function(e) {
    console.log('close');
    let m = e.popup._source;
    unclustered.removeLayer(m);
    clustered.addLayer(m);
    m.closePopup();
});

已更新 - 完整解决方案

https://jsfiddle.net/s2mnvL5w/24/

这是因为您从 clustered 组中删除了图层。再次添加到组后,它有一个新订单。

你可以这样做:

let popup;
const mkMarker = function(lat, lng, txt) {
    const m = L.marker(L.latLng(lat, lng));
    m.addTo(clustered);
  m.popupText = txt;
  m.on('click',(e)=>{
    var marker = e.target;
    var latlng = marker.getLatLng();
    var offset = [0,0];
    if(marker._preSpiderfyLatlng){
        latlng = marker._preSpiderfyLatlng;
    }else{
      offset= marker.options.icon.options.popupAnchor;
    }
    
    popup = L.popup({offset: offset}).setContent(marker.popupText).setLatLng(latlng).addTo(map)
  })
    return m;
};

并移除popupopen/close监听函数