1

I add a ol.layer.Vector OR ol.layer.Group with a mouse click in the map. By a new mouse click should the layer removed/deleted AND a new layer should be add. etc.

I have search and search and don't found a simple methode. There is all complicated or don't work, or with Ajax or JQuery (i don't use it).

Then i use this and that is the best solution:

if (map.getLayers().array_.length > 1) {
map.getLayers().array_.pop();
}

That works absolutelly perfect and is absolutelly simple. OpenLayers does not seem to have such a simple solution.

2 Answers 2

1

Properties ending with underscore are usually only available in debug builds of OpenLayers. getArray() returns an OpenLayers collection which has getLength() and pop() methods.

if (map.getLayers().getLength() > 1) {
  map.getLayers().pop();
}
2
  • OK, this works also. It's magic? You don't call the array? You call the object and pop it?
    – Suka
    Commented Dec 24, 2018 at 18:09
  • Yes, the collection object supports getLength() and pop() openlayers.org/en/latest/apidoc/…
    – Mike
    Commented Dec 24, 2018 at 18:12
0

I have found it myself. There is a getArray().

if (map.getLayers().getArray().length > 1) {
map.getLayers().getArray().pop();
}

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.