6

I'm using OpenLayers-3.8.2. I want to select a feature and then delete it. my code is as follow:

deleteFeature = function(event){
    var feature = event.selected[0];
    myLayer.getSource().removeFeature(feature);
}

selectInteraction = new ol.interaction.Select({
    condition: ol.events.condition.singleClick,
    toggleCondition: ol.events.condition.shiftKeyOnly,
    layers: [myLayer]
})

map.addInteraction(selectInteraction);

selectInteraction.on("select", deleteFeature, this);

This code delete the feature from source but it still showed in map.

How can I unselect a feature? Or how can I redraw a layer? Or how can I delete a featrue in openlayers-3?

  • Give some feedback on your questions! – Jonatas Walker Aug 10 '15 at 17:09
  • I update the question – itsme Aug 11 '15 at 8:08
2

You can use clear function. Change deleteFeature function as follow:

deleteFeature = function(event){
    var feature = event.selected[0];
    myLayer.getSource().removeFeature(feature);
    event.selected.clear();
}
17

On ol3 3.11.2, the event.selected.clear(); does not exist, so you need to use the selectionInteraction.getFeatures().clear();

  • That unselect all the features selected. If you have a selected feature before and then select a new one, will unselect all the features selected. – Jorge Rocha Mar 21 '17 at 10:43
1

In open layers 5.2, you can unselect a feature like this:

deleteFeature = function (event) { var feature = event.selected[0]; myLayer.getSource().removeFeature(feature); selectInteraction.getFeatures().remove(feature); }

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.