3

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

console.log(map.getLayers());

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 ?

2

You could try something like this:

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

will give you all your layers

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

 map.getLayers().getArray()[0].get('name')
  • 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.