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.
相关问题
- Leaflet popups for specific base maps
- leafletjs adding scrollable pop up?
- CreateFileMapping ends with “Not enough disk space
- Add text inside leaflet rectangle
- Fit map to bounds exactly
相关文章
- Is there a way to modify the entity mapping config
- change leaflet marker icon
- Prevent zooming out in leaflet R-Map?
- Multiple markers on the exact same position on a L
- Intermediate variable in a list comprehension for
- leaflet how to make dynamic picture popup using js
- Excel VBA - Dictionary - storing and retrieving va
- Fill polygon with pattern doesn't work with le
There is an easy way to get all functions related to leaflet or another by just typing the variable name or function name or leaflet layer name.
pls look at the below pic : https://i.stack.imgur.com/1LDz0.png
As Asborn hinted at you can access the maps layer object through any layer event object.
e.g.
to get the length you would need to count the keys and the easiest way is you use ECMA (chrome, node etc)
I'm surprised there isnt a API to get this info such as
:)
EDIT: actually there is: http://leafletjs.com/reference.html#layergroup
and it returns an array of the layers within that group.
Since my first answer, the
LayerGroup
has been extended with agetLayers()
method, making the solution straightforward:layerGroup.getLayers().length
.OLD REDUNDANT ANSWER:
Looking at the LayerGroup code, you should be able to get the number of layers by
layerGroup._layers.length
.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.If you want to stay within the public API, then you could call
eachLayer
and count the layers.