[英]Leaflet and MarkerCluster - don't perform clustering when there is a popup currently open?
我有一个 Leaflet + MarkerCluster 应用程序,它在地图上有一些标记和一些逻辑,当与标记关联的弹出窗口打开时(点击标记),将图像叠加添加到地图上。 我使用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 事件),我们最终会在地图上出现一个无法删除的重影图像。
在正常情况下,当用户关闭弹出窗口(或点击地图或其他标记上的其他地方)时,会生成“popupclose”事件并删除图像层:
markers.on('popupclose', removeOverlay);
function removeOverlay() {
if (window.overlay) {
map.removeLayer(overlay);
window.overlay=null;
}
目前,我被迫监听每个“animationend”事件(当地图上任何地方由 MarkerCluster 控制的集群发生任何变化时),然后删除图层并关闭弹出窗口,这是一种非常糟糕的方法,因为用户准备好缩放设置后,必须再次单击标记:
markers.on('animationend', function (e) {
removeOverlay();
map.closePopup();
});
如果您希望您的标记及其弹出窗口保留在地图上并且不再由 MarkerClusterGroup 处理,那么一个简单的解决方案是在标记单击时将其从该组中删除(并将其直接添加到地图中并打开弹出窗口)。
然后当 Popup 关闭时,将 Marker 添加回您的 MarkerClusterGroup(首先将其从地图中删除;如果它应该可见,Marker Cluster 会将其添加回来)。
至于丢失的“popupclose”事件,这确实可能是向 Leaflet.markercluster 或 Leaflet 报告的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.