使用复选框而不是单选按钮来控制传单层?

2022-01-12 00:00:00 leaflet javascript

有没有一种简单的方法来制作 传单层控件(L.control.layers) 使用复选框而不是单选按钮?

Is there an easy way to make a leaflet layers control (L.control.layers) use checkboxes rather than radio buttons?

我有多个 WMS 切片图层,我希望能够在地图上同时拥有多个图层.就上下文而言,WMS 切片图层包括水深测量和等高线(地形线),因此将两者同时可视化会提供更多信息,而不是仅仅让一些线漂浮在海洋中.

I have multiple WMS tile layers, and I'd like to be able to have more than one on the map at the same time. For context, the WMS tile layers include bathymetry and contours (topo lines), so it'd be more informative to visualize both at the same time, rather than just having some lines floating in the ocean.

在 传单示例中,它说图层控件足够聪明"可以知道分配单选按钮和复选框,但最好有更多自定义控件.

In the leaflet example it says the layers control is "smart enough" to know which to assign radio buttons and which checkboxes, but it'd be nice to have more customized control.

相关代码:

L.control.layers(WMS, null, {collapsed: false}).addTo(map);
其中 WMS 是多个 L.tileLayer.wms 层.

推荐答案

将您的 WMS 作为第二个参数(即作为叠加层)而不是 L.control 的第一个(底图)传递.layers.

Pass your WMS as the 2nd argument (i.e. as overlays) instead of the 1st (basemaps) of L.control.layers.

叠加使用复选框,而底图使用单选按钮.

Overlays use check boxes, whereas basemaps use radio buttons.

相关文章