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()


Is it possible?

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

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, privacy policy and cookie policy

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