【发布时间】:2019-12-20 09:00:05
【问题描述】:
我有一个 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();
});
【问题讨论】:
标签: javascript leaflet leaflet.markercluster