Search code examples
leafletgeojsonleaflet.draw

Leaflet draw delete features with property


Is it possible to delete multiple features from geojson at once by checking property value?

Using for example code below or using Leaflet draw?

function deleteArea() {
var layers = featureGroup.getLayers();
for (var i = 0; i < layers.length; i++) {

        if (layers[i].feature.properties.N == 1)
{
        "DELETE?" layer[i];
        };
}
};

I have big map and some markers have property: feature.properties.N=1. I can delete them using leaflet draw clicking one by one because I changed marker to red for those layers. But it takes some time.. Is it possible to to this at once?

enter image description here

Thank you very much for your time !


Solution

  • Sure you can simply remove the markers from the map / featuregroup with layer.removeFrom(featureGroup)

    function deleteArea() {
    var layers = featureGroup.getLayers();
    for (var i = 0; i < layers.length; i++) {
            if (layers[i].feature.properties.N == 1){
                layer[i].removeFrom(featureGroup);
            };
    }
    };
    

    PS: I prefer to use Leaflet-Geoman because it is more modern and is still being supported and gets new features