简体   繁体   English

Leaflet.js:将 map 缩放到可见标记

[英]Leaflet.js: zoom map to visible markers

I have a leaflet.js map with a collection of markers, which can be shown/hidden in a legend div done with plugin Leaflet.StyledLayerControl (I think this is not too relevant but just in case).我有一个带有标记集合的leaflet.js map,可以在使用插件Leaflet.StyledLayerControl完成的图例 div 中显示/隐藏(我认为这不太相关,但以防万一)。

I can capture the event when layers are shown or hidden and in that event I would like to zoom to fit all visible markers.我可以在图层显示或隐藏时捕获事件,在那种情况下我想缩放以适合所有可见标记。

There are a few questions in SO with a very similar title but most of them pretend to fit zoom to a known collection of markers, which is not my case (some other questions with very similar subject refer to Google maps, not leaflet). SO 中有一些标题非常相似的问题,但大多数问题都假装适合缩放到已知的标记集合,这不是我的情况(其他一些主题非常相似的问题指的是谷歌地图,而不是传单)。

So, the question would be:所以,问题是:

A) Can I set zoom to fit all visible markers? A) 我可以设置缩放以适合所有可见标记吗? B) or, how can I get an array of all visible markers to apply the other solutions seen? B) 或者,我怎样才能得到所有可见标记的数组来应用看到的其他解决方案?

This is how I create markers on map:这就是我在 map 上创建标记的方式:

const icon_general = L.divIcon({html: '<i class="fas fa-map-marker fa-2x"></i>', iconSize: [20, 20], className: 'node_icon'});

var node_10031 = L.marker([40.7174605,-3.9199218],{ icon: icon_general}).addTo(layer1);
node_10031.bindPopup('<h2>Title</h2>');
var node_10032 = L.marker([40.7184576,-3.9202692],{ icon: icon_general}).addTo(layer1);
node_10032.bindPopup('<h2>Title</h2>');
var node_10032 = L.marker([40.7361371,-3.9453966],{ icon: icon_general}).addTo(layer2);
node_10032.bindPopup('<h2>Title</h2>');

Layers are then hidden or shown and I can capture that event, that is where I want to modifiy zoom or loop through visible markers.然后隐藏或显示图层,我可以捕获该事件,这是我想要修改缩放或循环浏览可见标记的地方。

EDIT (final solution based on Seth Lutske response):编辑(基于Seth Lutske响应的最终解决方案):

This is my final solution, maybe it's less eficient as on each click it loops through all visible markers in map, but after some attempts this was the succesful one (I wasn't able to manage properly the individual events for add/remove layer):这是我的最终解决方案,也许它的效率较低,因为每次点击它都会循环遍历 map 中的所有可见标记,但经过一些尝试,这是成功的(我无法正确管理添加/删除层的各个事件) :

function setZoom2Visible()
{
    var visibleLayerGroup = new L.FeatureGroup();

    mymap.eachLayer(function(layer){
        if (layer instanceof L.Marker)
            visibleLayerGroup.addLayer(layer);
    });

    const bounds = visibleLayerGroup.getBounds();
    mymap.fitBounds(bounds);
}

$('.menu-item-checkbox input[type=checkbox]').click(function(){
    setZoom2Visible();
});

Create a featureGroup and add the markers to it:创建一个 featureGroup 并向其添加标记:

const myGroup = L.featureGroup([node_10031, node_10032, node_10033]);

On whatever event you're using to capture the markers being added to the map, you can get the bounds of the featureGroup and set the map's bounds to that:无论您使用什么事件来捕获添加到 map 的标记,您都可以获得 featureGroup 的边界并将地图的边界设置为:

function onMarkersAddedEventHandler(){
  const bounds = myGroup.getBounds();
  map.fitBounds(bounds);
}

Now if you're saying that each marker has its own toggle in the UI, it's a bit more complicated.现在,如果您说每个标记在 UI 中都有自己的切换开关,那就有点复杂了。 You'll have to attach an event to each marker's UI checkbox, and on change of that checkbox, add or remove the marker from the group, then reset the bounds:您必须将事件附加到每个标记的 UI 复选框,并在更改该复选框时,从组中添加或删除标记,然后重置边界:

const node_10031_checkbox = document.querySelector('input[type="checkbox"]#10031');

function onMarkersAddedEventHandler(e){
  if (e.target.checked){
    myGroup.addLayer(node_10031)
  } else {
    myGroup.removeLayer(node_10031)
  }
  const bounds = myGroup.getBounds();
  map.fitBounds(bounds);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM