I would like to iterate through all of the added layers in an ol.Map instance and only select the vector layers. So far I could get the individual layers with the a property of the resulting object of map.getLayers(). As you can see on the image below, a is an array and contains an object related to the corresponding layer.


The layers in the array are described with a letter (T for tile, V for vector). My question is, how to extract that information for my script to process?

I have read a related post with an answer using a .CLASS_NAME property, but the OpenLayers 3 ol.layer object doesn't have such attribute.

Any other approach to get the layer type or proven fact, that one can't get this property after the layer objects are constructed is considered as a correct answer.

2 Answers 2


If anyone gets stuck with identifying a layer's type in OpenLayers 3, there is a more sophisticated JavaScript method to achieve this. As layer objects are created with layer constructors, the native instanceof JavaScript function can be used to check for layer type.

As the identification of a layer type usually gets into an if or switch clause, one could easily check for the presence of the constructor's prototype object in the layer, instead of storing the type as a property on construction.

var groupLyr = new ol.layer.Group();
groupLyr instanceof ol.layer.Group;
groupLyr instanceof ol.layer.Vector;
  • Gabor, you should mark this one as the answer instead, so folks can see the better answer first!
    – janechii
    Commented Jul 17, 2016 at 19:09
  • Dude, this was a MAJOR save. tks, a zillion, a would have never found instanceof. UPVOTED
    – tony gil
    Commented Jul 22, 2017 at 2:21

I don't even think being able to extract the T and V, if possible, is gonna be reliable cross-browsers. Might just be a browser interpretation thing. In any case, if you have access to it, I would just add your own attribute when you're creating the layer:

var foo_layer = new ol.layer.Vector(...);
foo_layer.layer_type = 'vector';

Then you can use it later in your code:

// 'vector'

Would that work for you?

  • very useful for other situations as well. UPVOTED
    – tony gil
    Commented Jul 22, 2017 at 2:20

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.