Skip to content Skip to sidebar Skip to footer

Remove Array Of Features (openlayers 4)

Am trying to remove an array of features from OpenLayers 4 map. I don't want to clear all features in the source. Added few selected features to an array. Currently, am iterating

Solution 1:

Your solution is one solution but you can also use docketSource.getFeaturesCollection() that returns a ol.Collection of ol.Features. Then, you can manipulate the returned collection and use the function remove

So, you could do docketSource.getFeaturesCollection().remove(yourfeature); However, if you need to loop through the collection, you will use forEach method of the collection and it would be similar to your solution.

Look at ol.Collection methods to see if they could fit better with your purpose.

Solution 2:

Assuming your source is an ol.source.Vector you can execute clear() by doing:

source.clear()

I'm a little confused with what you're asking, but you've mentioned "selected features". Perhaps this is what you're after?

var select = new ol.interaction.Select();
select.getFeatures().forEach(function(feature){
   docketSource.removeFeature(feature);
});

See http://openlayers.org/en/v3.0.0/apidoc/ol.source.Vector.html#clear for more information.

UPDATE: There is a removeFeatures function in OL 2.13.1.

removeFeatures: function(features, options)

Remove features from the layer. This erases any drawn features and removes them from the layer’s control. The beforefeatureremoved and featureremoved events will be triggered for each feature. The featuresremoved event will be triggered after all features have been removed. To suppress event triggering, use the silent option.

See: http://dev.openlayers.org/releases/OpenLayers-2.13.1/doc/apidocs/files/OpenLayers/Layer/Vector-js.html#OpenLayers.Layer.Vector.removeFeatures

Solution 3:

removeFeature is the only API function to remove some features from an ol.source.Vector . (See https://github.com/openlayers/openlayers/blob/v4.6.5/src/ol/source/vector.js )

If you have problems with lots of events fired or something like that, you could extend the ol.source.Vector class and implement a removeFeatures function, for example modifying the clear function or merging clear with removeFeature.

Post a Comment for "Remove Array Of Features (openlayers 4)"