【发布时间】:2021-09-13 19:32:41
【问题描述】:
我正在尝试在 Leaflet Javascript 库中添加图层的控件;供参考,请参阅:https://leafletjs.com/reference-1.7.1.html#control-layers。我的代码大部分工作正常:我有一个layerControl 对象,它使用baseLayers 进行初始化,但还没有覆盖。然后我添加一个layer_USA_Counties Overlay,如下面的代码所示。到目前为止一切顺利。
但我需要能够将新的isochronesGroup 叠加层添加到 layerControl 对象。该代码工作正常,只是在后续代码执行中 isochronesGroup 不断添加到控件中 - 请参阅此屏幕截图。我可以使用注释掉的命令删除控件,但它当然也会删除所有isochronesGroup 控件,因此根本不显示它。请注意:加载的图层及其控件不必是layerGroup() 类型。
//globally set
var baseLayers = {
"Grayscale": grayscale,
"Streets": streets
};
layerControl = L.control.layers(baseLayers, null, { collapsed: false }).addTo(map);
//add a layer from a WMS call and add to the layerControl object
map_LayerGroup = L.layerGroup().addLayer(layer_USA_Counties);///.addTo(map);
layerControl.addOverlay(map_LayerGroup, "USA Counties");
//Following code gets executed again and again based on user interaction
isochronesGroup = L.layerGroup().addLayer(route_lines).addTo(map);
layerControl.addOverlay(isochronesGroup, "Isochrones");
///layerControl.removeLayer(isochronesGroup); //WORKS If already created. Or errors
【问题讨论】:
标签: javascript leaflet