Leaflet.js-如果所有弹出窗口都关闭,则将视图返回到起始位置

时间:2018-08-24 08:29:14

标签: javascript leaflet

我设法在工作地图上获得自定义图标标记,并且弹出窗口在工作等。

但是我无法解决一件事。目前,我将地图放大并在弹出窗口打开时居中。我想让地图在所有弹出窗口都关闭时返回到起始位置。

我确实找到了一个片段,该片段使地图在关闭时返回到起始位置,但是当您从一个弹出窗口单击到另一个弹出窗口时触发了该片段。我猜想它确实满足了我的要求,但这对可用性不是很好。

我有一个标准设置,但添加了以下内容:

function markerOnClick(e) {
    var latLngs = [e.target.getLatLng()];
    var markerBounds = L.latLngBounds(latLngs);
    maphitchin.fitBounds(markerBounds);
}

1 个答案:

答案 0 :(得分:0)

一种方法是使用popupopen and popupclose events of L.Map ...来统计地图中打开了多少个弹出窗口。

var openPopupsCount = 0;
map.on('popupopen',  function(){ openPopupsCount++; });
map.on('popupclose', function(){ openPopupsCount--; });

...并在这样的popupclose事件中,当计数达到零时,运行任意操作:

map.on('popupclose', function(){ 
    openPopupsCount--;
    if (openPopupsCount === 0) {
        map.fitBounds(initialBounds);
    }
});
相关问题