[GIS] How to add ol.layer.Vector source dynamically

openlayers

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?

Best Answer

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.