如何为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/
有什么想法吗?
推荐答案
对于矢量几何无法完成.
Cannot be done for vector geometries.
zIndex
是HTMLElement
的属性,矢量几何(线和多边形)呈现为SVG元素,或以编程方式呈现为<canvas>
绘制调用.这两种方法没有zIndex
的概念,因此唯一可行的方法是将元素推到SVG组或<canvas>
绘制序列的顶部(或底部).
zIndex
is a property of HTMLElement
s, and vector geometries (lines and polygons) are rendered as SVG elements, or programatically as <canvas>
draw calls. Those two methods have no concept of zIndex
, so the only thing that works is pushing elements to the top (or bottom) of the SVG group or <canvas>
draw sequence.
还请注意,L.GeoJSON
只是L.LayerGroup
的特定类型,在您的情况下包含L.Polygon
的实例.此外,如果您阅读有关L.LayerGroup
上setZIndex()
方法的小册子,请参见:
Also, remind that L.GeoJSON
is just a specific type of L.LayerGroup
, in your case containing instances of L.Polygon
. Furthermore, if you read Leaflet's documentation about the setZIndex()
method on L.LayerGroup
:
在此组中包含的每个图层上调用
setZIndex
,并传递z-index.
Calls
setZIndex
on every layer contained in this group, passing the z-index.
那么,L.Polygon
是否具有setZIndex()
方法?否.因此,在其所在的小组中称其为无能为力.但是,它将影响该组中包含的所有L.GridLayer
.
So, do L.Polygon
s have a setZIndex()
method? No. So calling that in their containing group does nothing. It will have an effect on any L.GridLayer
s contained in that group, though.
回到您的问题:
我希望某些图层始终位于其他图层之上,无论它们按什么顺序添加到地图中.
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.
看起来您要找的东西是地图窗格.请阅读地图窗格教程.
Looks like the thing you're looking for is map panes. Do read the map panes tutorial.
这篇关于如何为geoJson图层设置zIndex图层顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!