【发布时间】: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