4

How to set source dynamically to a layer vector in OpenLayers 3

like this

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

vectorLayer.setSource(another_source);

Is it possible?

1
  • 1
    To complete Sefa answer, you can also get the source doing var source = vectorLayer.getSource() and manipulate it.
    – ThomasG77
    Commented Dec 10, 2014 at 14:29

1 Answer 1

5

There is no method for setting source at api, http://openlayers.org/en/v3.0.0/apidoc/ol.layer.Vector.html. However you can define source;

var source = new ol.source.Vector();

and set source at the beginning;

var vectorLayer=  new ol.layer.Vector({

source: source

});

Then you are free to add or remove features to source dynamically by using addFeature and removeFeature methods without a need of dynamic change of source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.