问题描述
我必须修改使用传单层控制的现有应用程序-启动地图时,我需要显示基本层之一.有没有办法从JS脚本的图层控件中调用某些函数-如control.select(1)....?如果没有,如何以与控件相同的方式添加平铺图层-当我在地图初始化期间添加新的L.TileLayer时,不会被手动图层控件选择更改覆盖吗?
I have to modify existing application, where leaflet layers control is used - I need to display one of the base layers when the map is initiated. Is there a way, how to call some function from the layers control from JS script - something like control.select(1) ....? If not, how can add a tile layer in the same way as it is done by the control - when I add new L.TileLayer during map init, it's not overwritten by manual layers control selection change?
推荐答案
您可以尝试模拟用户单击"Leaflet Layers控件",但是有一种更简单的方法来实现您最初描述的内容.
You could try to emulate a user click on the Leaflet Layers Control, but there is a much more simple way to achieve what you initially describe.
如果该图层是基础图层或图层控件"的叠加层,后者将自动更新其状态(如果添加了基本层,则将单选相应的单选按钮;对于叠加层,将选中相应的复选框)
Normally by simply adding a layer to the map (e.g. myTileLayer.addTo(map)
), if that layer is part of the base layers or overlays of the Layers Control, the latter will automatically update its status (if you added a base layer, the radio buttons will be selected accordingly; for an overlay, the corresponding checkbox will be ticked).
现在,我不确定我是否正确理解了您的最后一部分(在地图初始化期间添加新的L.TileLayer时,不会被手动图层控件选择更改覆盖").
Now I am not sure I understood properly your last part ("when I add new L.TileLayer during map init, it's not overwritten by manual layers control selection change").
如果您的意思是由于添加的图块层没有被图层控件"更改而出现意外行为,则可能是由于您没有重新使用图层控件"知道的图块图层:使用new L.TileLayer
,但重复使用基本层或覆盖层之一.
If you mean you have an unexpected behaviour because the Tile Layer you added is not changed by the Layers Control, it may be due to the fact that you are not re-using a Tile Layer that the Layers Control knows: do not use new L.TileLayer
, but re-use one of the base layers or overlays.
例如:
var baselayers = {
"Tile Layer 1": L.tileLayer(/* ... */),
"Tile Layer 2": L.tileLayer(/* ... */),
"Tile Layer 3": L.tileLayer(/* ... */)
};
var overlays = {};
L.control.layers(baselayers, overlays).addTo(map);
baseLayers["Tile Layer 1"].addTo(map);
这篇关于如何使用JS和传单层控件更改基础层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!