Leaflet.js: is it possible to filter geoJSON features by property?
filter, leaflet
Solution
Please, see Using GeoJSON with Leaflet - Leaflet - a JavaScript library for interactive maps.
Yes, you can, just add a filter function like:
L.geoJson(someFeatures, {
filter: function(feature, layer) {
return feature.properties.show_on_map;
}
}).addTo(map);
Or if you want dynamic updating, there's a great answer in this other SO question: Leaflet: Update GeoJson filter?
Problem
I'm looking around and I see a lot of information about how to show/hide layers. That's cool, but since I can add arbitrary properties to GeoJSON features, I kind of expect to be able to filter them accordingly. For instance, if I have features 1, 2 & 3 with these properties: - small | red | sweet - large | green | sour - small | red | hot How would I filter them by size? Or by color or flavor?