I want to remove existing vector layers on the map and


returns me ol.source.Source object as it stated in the API.

I cannot find the way how to jump from ol.source.Sourceto ol.source.Vector object. Everything I find in answers was to use something like this


which assumes that getSource() returns ol.layer.Vector I believe (generates error).

How to get to ol.layer.Vector from map?

1 Answer 1


The function map.getLayers() gives you a collection of all layers in the map, so you first have to select the vector layers out of this collection. This would be one way:

let vectorLayerArray = new Array();
map.getLayers().getArray().some(function(layer, i, array) {
  if (layer instanceof ol.layer.Vector) {
}, this);

You would now have the array vectorLayerArray with all available vector layers. You can now iterate of that array an get the vector source from theses layers.

vectorLayerArray.forEach(function(aVecLayer) {

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.