[GIS] How to remove last drawing feature in OpenLayers 3

openlayers

I have already found Undoing last point when drawing linestring in OpenLayers 3? (with working Plunker) but it can't undo misclicks when drawing polygon. So what I want is remove last drawing.

Does anyone know how to do that?

Best Answer

You can use something like this -

var features = vectorSource.getFeatures();
var lastFeature = features[features.length - 1];
vectorSource.removeFeature(lastFeature);

Note that you need to vectorSource.getFeatures() every time you want to remove the last feature or use it in a function.