简单的方法来获取Leaflet LayerGroup的大小? [英] Easy way to get size of Leaflet LayerGroup?
问题描述
是否有一种简单的方法来获取传单LayerGroup 中的层数?快速浏览文档说明目前没有.
Is there a straightforward way to get the number of layers in a Leaflet LayerGroup? A quick glance at the documentation says there isn't at the moment.
推荐答案
自从我的第一个答案以来,LayerGroup
已通过getLayers()
方法进行了扩展,使解决方案变得简单易懂:layerGroup.getLayers().length
.
Since my first answer, the LayerGroup
has been extended with a getLayers()
method, making the solution straightforward: layerGroup.getLayers().length
.
旧冗余答案:
查看 LayerGroup代码,您应该能够通过layerGroup._layers.length
获取层数.
Looking at the LayerGroup code, you should be able to get the number of layers by layerGroup._layers.length
.
但是请注意,实现者的_layers
意图是私有的,因此我想您不确定这在将来的版本中是否可以使用.
But note that _layers
by the implementors is intended to be private, so I guess you cannot be sure this will work in future versions.
如果要保留在公共API中,则可以调用eachLayer
并计算层数.
If you want to stay within the public API, then you could call eachLayer
and count the layers.
这篇关于简单的方法来获取Leaflet LayerGroup的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!