1

I've this:

var AllProjectArray = L.geoJSON();

The using addData()

AllProjectArray.addData(somefeatures)

Now I've my AllProjectArray filled and working

Now I want to remove from AllProjectArray one specific feature, something like AllProjectArray.removeData(thisfeatures).

I've tried with

AllProjectArray.removeLayer(..);

whit no luck. Where I'm wrong?

2

GeoJSON layer is in Leaflet a group layer where each feature is member layer of this group. You can iterate through all members by eachLayer group layer method.

If you want to remove particular feature/member from GeoJSON layer, you have to somehov identify it, by id or some property. Let's say we want to remove feature with particular feature id.

One possibilty is that each time we want to remove feature, we loop through all members od GeoJSON layer and remove the one with desired id. Function for removing feature could look something like this:

function removeFeature(layerGroup, featureId) {
  layerGroup.eachLayer(function(layer){
    if (layer.feature.id == featureId) {
      layerGroup.removeLayer(layer);
    }
  });
}

We would then remove feature with desired id by call

removeFeature(AllProjectArray, id);

To avoid looping through all features at each remove, we can create object which connects feature id with internal Leaflet layer id:

var AllProjectArray = L.geoJSON();
AllProjectArray.addData(somefeatures);

var layerID = {};
AllProjectArray.eachLayer(function(layer) {
  layerID[layer.feature.id] = AllProjectArray.getLayerId(layer);
});

Removing feature with desired id is then done without any iteration with simple call:

AllProjectArray.removeLayer(layerID[id]);
  • Thanks for elucidation – Taras Nov 14 at 21:39
  • Thanks, I managed to solve my problem with your suggestion! – Cenere Blu Nov 15 at 12:12
  • Then you could also mark answer as accepted (not only upvoted) so it will be clear that problem is solved. – TomazicM Nov 15 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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