0

I want to remove existing vector layers on the map and

map.getLayers().getSource();

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

map.getLayers().getSource().getFeatures()

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

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

1 Answer 1

2

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) {
    vectorLayerArray.push(layer);
  }
}, 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) {
  aVecLayer.getSource();
})

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.