Reputation: 592
I have a Leaflet map with a layer called flickrpics that is loaded dynamically in geojson based on the bbox of the current map view. I would like to get a simple count of the number of markers in that layer, so that I can display it next to the layer label in the layer control. I've tried things like flickrpics.length but it's saying undefined.
Apologies, pretty new to Leaflet and javascript!
Upvotes: 3
Views: 8275
Reputation: 26
Reference: https://leafletjs.com/reference-1.3.4.html#geojson
Methods inherited from LayerGroup:
getLayers() Layer[]
Returns an array of all the layers added to the group.
var pins = L.geoJson(geojsonFeature, {}).addTo(map);
var totalPins = pins.getLayers().length;
Upvotes: 1
Reputation: 2550
If you are using L.geoJson for geoJSON loading, you can use the onEachFeature to count the number of objects in the geoJSON layer. Something like:
var counter = 0;
function onEachFeature(feature, layer) {
counter++;
}
L.geoJson(geojsonFeature, {
onEachFeature: onEachFeature
}).addTo(map);
See http://leafletjs.com/examples/geojson.html for more information.
Upvotes: 3