Remove selected feature Openlayers 3
Yes, you can remove selected feature.
var draw;
var featureID = 0;
var singleClick;
var selectedFeatureID;
// First some change in this function.
function addInteraction() {
map.removeInteraction(singleClick);
draw = new ol.interaction.Draw({
source: source,
type: ("LineString")
});
// Create drawend event of feature and set ID to feature
draw.on('drawend', function (event) {
featureID = featureID + 1;
event.feature.setProperties({
'id': featureID
})
})
map.addInteraction(draw);
}
Then Change in select Function as follow:
function addSelect() {
map.removeInteraction(draw);
singleClick = new ol.interaction.Select();
map.addInteraction(singleClick);
singleClick .getFeatures().on('add', function (event) {
var properties = event.element.getProperties();
selectedFeatureID = properties.id;
});
}
Then call this function on REMOVE button click
function removeSelectedFeature() {
var features = source.getFeatures();
if (features != null && features.length > 0) {
for (x in features) {
var properties = features[x].getProperties();
console.log(properties);
var id = properties.id;
if (id == selectedFeatureID) {
source.removeFeature(features[x]);
break;
}
}
}
}
With this code you can remove any selected feature. If it is Line,Point, Polygon etc.