Getting the number of markers in a layer in a leaflet

I have a Leaflet map with a flickrpics layer that dynamically loads into geojson based on the bbox of the current map view. I would like to get a simple count of the number of markers in this layer so that I can display it next to the layer label in the level control. I tried things like flickrpics.length, but it said undefined.

Apologies, quite new for Flyers and javascript!

+2
source share
1 answer

If you use L.geoJson to load geoJSON, you can use onEachFeature to count the number of objects in the geoJSON layer. Sort of:

var counter = 0;

function onEachFeature(feature, layer) {
counter++;
}

L.geoJson(geojsonFeature, {
onEachFeature: onEachFeature
}).addTo(map);

See http://leafletjs.com/examples/geojson.html for more details .

+3
source

All Articles