slevin
slevin

Reputation: 3888

Create clean vector layer without source

In Openlayers 3.9.0 I am implementing a code to draw features and grab their geometry type and coordinates.

I have use to OL2 when you could create a "clean" vector layer without a source, so the user could just create new features, without seeing the old, saved ones.

In OL3 I cannot create a vector layer without a source. I dont get any errors, but when I create a pint, I dont get it on the map, even though I set a style

layerVector = new ol.layer.Vector({
    style:myStyle
});

In addition a vector layer must have a source, so I can do this

var features = sourceVector.getFeatures();

and get the newly created features.

Any solution to a clean vector layer without a source or at least without loading the saved features?

Thanks

Upvotes: 2

Views: 2609

Answers (1)

pavlos
pavlos

Reputation: 3081

just initialise your vector layer with an empty source like so:

layerVector = new ol.layer.Vector({
    source:new ol.source.Vector()
});

then to clean your layer from any features

var vecSource = layerVector.getSource();
var featsToRemove = vecSource.getFeatures();
    for (var f=0;f<featsToRemove.length;f++)
    {
    vecSource.removeFeature(featsToRemove[f]);
    }

or better as @Alvin Lindstam suggests

var vecSource = layerVector.getSource();
vecSource.clear() 

I have not test it but it should work.

Upvotes: 2

Related Questions