我正在使用 Leaflet.StyledLayerControl插件并想设置我的图层,以便多边形总是被推到后面,多边形上方的线和线上方的点。
我在这里搜索了解决方案,但大多数都引用了 tilelayers 或 map Pane (我认为它们只适用于另一个版本的 leaflet 1.0)。
我希望能够打开和关闭线并让它们始终低于点(与折线下方的多边形相同)。
我猜我必须用 setZIndex
或 bringToFront()
做点什么,但我不确定从哪里开始。
如有任何帮助,我们将不胜感激。谢谢。
最佳答案
真正简单的解决方案是使用 Leaflet 1.0,它为您提供 map.createPane("paneName")
功能。所以你创建像“polygonsPane”,“linesPane”和“pointsPane”,你使用它们的pane
指定给每个矢量/形状层。选项。
设置后,您可以将它们添加到 map 或从 map 中删除它们,并且它们将始终插入到指定的 Pane 中,因此尊重 Pane 的顺序。
// Create necessary panes in correct order (i.e. "bottom-most" first).
map.createPane("polygonsPane");
map.createPane("linesPane");
map.createPane("pointsPane");
L.polygon([/* coords */], { pane: "polygonsPane" }).addTo(map);
L.polyline([/* coords */], { pane: "linesPane" }).addTo(map);
L.circleMarker([/* coords */], { pane: "pointsPane" }).addTo(map);
演示:https://jsfiddle.net/3v7hd2vx/51/
使用 Leaflet 0.7,您知道所有矢量图层都是同一个 SVG 容器的一部分,在添加到 map 时被附加,因此它们出现在所有先前添加的矢量之上。然后您必须使用 bringToFront()
和/或 bringToBack()
将它们重新排序为您需要的任何顺序。
您可能对该案例的帖子感兴趣:https://gis.stackexchange.com/questions/166252/geojson-layer-order-in-leaflet-0-7-5/167904#167904
关于javascript - 传单叠加顺序(点、线和多边形),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38599280/