【问题标题】:Is there a way in Leaflet to reset the function from a L.easybutton?Leaflet 中有没有办法从 L.easybutton 重置功能?
【发布时间】:2020-12-21 21:49:01
【问题描述】:

使用 Leaflet,我将一个函数链接到 L.easyButton,该函数将 geoJson 多边形图层添加到地图,并激活该图层的不同功能“onEachFeature”。其中,单击某个功能时的一个功能是使用该功能创建一个单独的geoJson多边形图层并将其添加到特定的LayerGroup。我想要的是,点击一个功能后,不同的功能“onEachFeature”将不再被激活,如果他/她想再次按下L.easyButton,用户将需要再次按下。一切正常,除了停用“onEachFeature”功能。我尝试使用map.removeControl()、map.removeLayer() 并直接删除变量,但这些选项都不起作用。

我的代码是:

L.easyButton( '<strong>Sélectionner une commune</strong>', function(){
    var communes_geojson = $.getJSON("data/Adm/Adm_Communes_4326.geojson",function(communes){
        var communes_new = L.geoJson(communes, {
            style:  stylelayer.communes,    
            onEachFeature: function addMyData (feature,layer){
                layer.on({
                    click: selectcommune
                });
            }
        });
        map.addControl(communes_new);
    });
    info.addTo(map);
},
{position: 'topright'}).addTo(map);

function selectcommune(e) {
    var layer=e.target;
    var feature = e.target.feature;
    var selectcommune1 = L.geoJson(feature, {
        style:  stylelayer.highlight
    }).addTo(map);

    control_map2.addOverlay({
        name:feature.properties.NameFRE,
        layer:selectcommune1
    });
    
    // don't work
    
    // map.removeControl(communes_geojson);
    // map.removeControl(communes_new);
    // map.removeControl(layer);
    
    // map.removeLayer(communes_geojson);
    // map.removeLayer(communes_new);
    // map.removeLayer(layer);  
    
    // delete communes_geojson;
    // delete communes_new;
    // delete layer;

感谢您的帮助。

【问题讨论】:

    标签: javascript leaflet geojson layer


    【解决方案1】:

    selectcommune 中,一旦你做了你想做的事(设计你的功能并添加你的覆盖),你需要返回并取消你用 L.easyButton 添加的onEachFeature 函数:

    L.easyButton( '<strong>Sélectionner une commune</strong>', function(){
        var communes_geojson = $.getJSON("data/Adm/Adm_Communes_4326.geojson",function(communes){
            var communes_new = L.geoJson(communes, {
                style:  stylelayer.communes,    
                onEachFeature: function addMyData (feature,layer){
                    layer.on({
                        click: e => selectcommune(e, communes_new)
                    });
                }
            });
            map.addControl(communes_new);
        });
        info.addTo(map);
    },
    
    
    function selectcommune(e, data) {
        var layer=e.target;
        var feature = e.target.feature;
        var selectcommune1 = L.geoJson(feature, {
            style:  stylelayer.highlight
        }).addTo(map);
    
        control_map2.addOverlay({
            name:feature.properties.NameFRE,
            layer:selectcommune1
        });
    
        data.eachLayer( layer => layer.on = () => {} )
    }
    

    所以你点击了简单按钮,layer.on 被设置为每个功能触发selectcommune。当它触发时,它会删除 layer.on 函数(或覆盖它以不执行任何操作),直到您再次点击 easybutton。

    【讨论】:

    • 您好,感谢您的回答。由于以下错误,此解决方案不适用于我:“未捕获的 ReferenceError:未定义公社”。你知道为什么吗?
    • 我的错,当然它没有定义。最好将communes_new 作为参数传递给selectcommune。然后您可以调用eachFeature 并取消该功能。我必须在传单文档中进行一些挖掘才能找到它,但我还没有对其进行测试。如果您在 codepen 或 codesandbox 上创建一个可重现的示例,我可以分叉它并确保它有效。基本上,L.geoJson 继承自 L.FeatureGroup,而 L.FeatureGroup 继承自 L.LayerGroup,因此 eachLayer 应该可以在您的 communes_new 上使用
    猜你喜欢
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-22
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多