I'm working with the javascript library OpenLayers 3.0. I try to have the listing of the layers of my map.

If I do


I've got the array [a] my four layers :

enter image description here

But I don't find in that, the name of my layers.

How can I take the name and print them in an alert ?


You could try something like this:

var layers = map.getLayers();
var length = layers.getLength();
for (var i = 0; i<length; i++) {
  • 1
    It's perfect, thank you, the good code is : map.getLayers().item(i).get('name') – Geo-x May 21 '15 at 7:20

will give you all your layers

if the name of your attribute is name, then for the first element of you list of layers:

  • Thank you juls, but if I do that Mozilla say :"TypeError: map.getLayers(...).getArray[0] is undefined" – Geo-x May 20 '15 at 15:05
  • what version of ol3 are you using, and is your var 'map' global? – julsbreakdown May 20 '15 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.