我有一个 Leaflet + MarkerCluster 应用程序,它在 map 上有一些标记,并在打开与标记关联的弹出窗口时(单击标记时)将图像叠加层添加到 map 的一些逻辑。我使用 bindPopup()
将弹出窗口添加到标记。
markers.on('popupopen', function (e) {
var b = e.layer.boundary;
if (b) {
//take some parameters for the overlay from an array that is passed as a
//property of the marker
var image = b[0];
var LatStart = b[1];
var LngStart = b[2];
var LatEnd = b[3];
var LngEnd = b[4];
//doOverlay() creates the overlay
var overlay = doOverlay(image,LatStart,LngStart,LatEnd,LngEnd);
//add the overlay to the map
map.addLayer(overlay);
//make the overlay widely-accessible as a window property
window.overlay = overlay;
}
doOverlay() 包含一些创建 ImageOverlay 的基本逻辑:
function doOverlay(image,LatStart,LngStart,LatEnd,LngEnd) {
var bounds = new L.LatLngBounds (
new L.LatLng(LatStart,LngStart),
new L.LatLng(LatEnd,LngEnd));
var overlay = new L.ImageOverlay(image,
bounds, {
pane: 'general'
});
return overlay
}
但是,因为我使用 MarkerCluster 将标记分组到集群中,如果用户单击标记,获取弹出窗口并创建叠加层,然后他们缩小,就会发生集群,标记被集群,弹出窗口消失(但没有生成 popupclose 事件),我们最终在 map 上覆盖了一个无法删除的重影图像。
在正常情况下,当用户关闭弹出窗口(或单击 map 上的其他地方或另一个标记)时,会生成“popupclose”事件,我会删除图像层:
markers.on('popupclose', removeOverlay);
function removeOverlay() {
if (window.overlay) {
map.removeLayer(overlay);
window.overlay=null;
}
Currenrly,我被迫监听每个“animationend”事件(本地图上任何地方由 MarkerCluster 控制的集群发生任何变化时),然后删除图层并关闭弹出窗口,这是一个非常糟糕的方法,因为当用户准备好缩放设置时,他们必须再次点击标记:
markers.on('animationend', function (e) {
removeOverlay();
map.closePopup();
});
最佳答案
如果您希望您的 Marker 及其 Popup 保留在 map 上并且不再由 MarkerClusterGroup 处理,那么一个简单的解决方案是在单击 Marker 时将其从该组中删除(并将其直接添加到 map + 打开 Popup ).
然后当 Popup 关闭时,将 Marker 添加回您的 MarkerClusterGroup(首先将其从 map 中删除;如果 Marker Cluster 应该可见,则会将其添加回来)。
至于缺少的“popupclose”事件,这确实是一个需要报告给 Leaflet.markercluster 或 Leaflet 的问题。
关于javascript - Leaflet 和 MarkerCluster - 当前打开弹出窗口时不执行聚类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57483921/