0

I'm trying to add a filter to my GeoJSON data. But I can't figure out where I should add this in my code. Pins are added to shipLayergroup. I want to filter by property KODE. (e.g. KODE = 1)

   var pins = L.geoJson(data, {
        pointToLayer: function (feature, latlng) {
        return new L.CircleMarker(latlng, {radius: 8, 
                                            fillOpacity: 1, 
                                            color: 'black', 
                                            fillColor: getColor(feature.properties.KODE), 
                                            weight: 1,});
        }, 
    }).addTo(shipLayergroup);

I want to filter my data so that I can count the numbers of features with a specific property afterwards with:

    var totalPins = pins.getLayers().length;
    console.log(totalPins)
2
  • When do you want to do the filtering? When displaying features or only when counting?
    – TomazicM
    Aug 9 at 13:57
  • The data is added to shipLayergroup inside a Moveend. Data is updated every time a user move the map, therefore the filter should happen on the same time
    – LAC
    Aug 10 at 7:01

1 Answer 1

0

This is the way:

    var pins = L.geoJson(data, {,
    filter: picnicFilter,
    pointToLayer: function (feature, latlng) {
    return new L.CircleMarker(latlng, {radius: 8, 
                                        fillOpacity: 1, 
                                        color: 'black', 
                                        fillColor: getColor(feature.properties.KODE), 
                                        weight: 1,});
    }, 
}).addTo(shipLayergroup);

Function:

function picnicFilter(feature) {
if (feature.properties.KODE === 1) return true}

Based on: Filtering GeoJSON data to include in Leaflet map

Not the answer you're looking for? Browse other questions tagged or ask your own question.