简体   繁体   English

如何在显示或隐藏传单图层组时收到通知?

[英]How to get notified when an Leaflet LayerGroup is shown or hidden?

I want to get notified whenever the user clicks the "Cities" checkbox in the layer controls - which shows or hides the city markers in the linked example .我想在用户单击图层控件中的“城市”复选框时收到通知 - 这会显示或隐藏链接示例中的城市标记。 Here is a JSFiddle to play around with.这是一个可以玩的JSFiddle

传单截图

I saw that LayerGroup derives some events from FeatureGroup .我看到LayerGroupFeatureGroup派生一些事件 Please correct me if I am wrong: As far as I understand showing and hiding the markers is not the same as layeradd and layerremove ?!如果我错了,请纠正我:据我所知,显示和隐藏标记与layeraddlayerremove 不同?!

The input element holds no id to bind to: input元素没有要绑定到的id

<label>
    <input class="leaflet-control-layers-selector" type="checkbox" checked="">
    <span> Cities</span>
</label>

How can I be notified when the markers' visibility is toggled?当标记的可见性被切换时,我如何得到通知?

You could use jQuery to select the input element, using its class:您可以使用 jQuery 来选择输入元素,使用它的类:

$('.leaflet-control-layers-selector').click(function(){
    alert('something')
});

Furthermore, if you have more than one layer, you can check if the map contains one layer when you click one of the checkboxes.此外,如果您有多个图层,则可以在单击其中一个复选框时检查地图是否包含一个图层。

$('.leaflet-control-layers-selector').click(function(){
    if(map.hasLayer(cities)) alert('something');
});

UPDATE更新

You can also use map events overlayadd and overlayremove, like this:您还可以使用地图事件 overlayadd 和 overlayremove,如下所示:

map.on({
    overlayadd: function(e) {
        if (e.name === 'Cities') alert('added');
    },
    overlayremove: function(e) {
        if (e.name === 'Cities') alert('removed');
    }
});

Here's an updated JSFiddle of your example: http://jsfiddle.net/pufanalexandru/g54efr69/1/这是您示例的更新 JSFiddle: http : //jsfiddle.net/pufanalexandru/g54efr69/1/

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

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