如何为 geoJson 图层设置 zIndex 图层顺序? [英] How to set the zIndex layer order for geoJson layers?
问题描述
我希望某些图层始终位于其他图层之上,无论它们以何种顺序添加到地图中.我知道 bringToFront()
,但它不符合我的要求.我想根据属性动态设置 zIndex.
I would like to have certain layers to be always on top of others, no matter in which order they are added to the map.
I am aware of bringToFront()
, but it does not meet my requirements. I would like to set the zIndex dynamically based on properties.
Leaflet 有方法 setZIndex()
,但这显然不适用于 geoJson 层:https://jsfiddle.net/jw2srhwn/
Leaflet has the method setZIndex()
, but this apparently does not work for geoJson layers:
https://jsfiddle.net/jw2srhwn/
有什么想法吗?
推荐答案
这是在 Leaflet 1.0(与 0.x 版本相比)中实现用户定义的窗格"的原因之一.
This is one of the reason for the implementation of user defined "panes" in Leaflet 1.0 (compared to versions 0.x).
- 创建窗格:
var myPane = map.createPane("myPaneName")
- 如有必要,设置窗格元素的类/z-index:
myPane.style.zIndex = 450
(参考内置窗格的z-index值) - 创建图层时,指定它们的目标
pane
选项:L.rectangle(corners, { pane: "myPaneName" })
- 通过
L.geoJSON
工厂构建时,您可以使用onEachFeature
选项循环遍历您的功能,以使用指定的目标pane
.
- Create panes:
var myPane = map.createPane("myPaneName")
- If necessary, set the class / z-index of the pane element:
myPane.style.zIndex = 450
(refer to z-index values of built-in panes) - When creating your layers, specify their target
pane
option:L.rectangle(corners, { pane: "myPaneName" })
- When building through the
L.geoJSON
factory, you can loop through your features with theonEachFeature
option to clone your layers with specified targetpane
.
演示:https://jsfiddle.net/3v7hd2vx/90/
这篇关于如何为 geoJson 图层设置 zIndex 图层顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!