I'm trying to get name of layers added to OpenLayers v6.14.1, I found some methods but they were very old for instance: "map.layers" and one other method I found was :

let addLayers = map.getLayers();
let length =  addLayers.getLength();
for(let i=0;i<length;i++){
   console.log('layers added already name = ', map.getLayers().item(i).get('name'))

but when I used above method I got "undefined" message.

I'm going to know how I can get name of layers added to Openlayers map v6.14.1?

my layers are WMS

1 Answer 1


Layers in OL do not have names by default, but you can give layer a name by setting some custom layer property, like for example using custom option name: 'My layer name'. Then your code above will work.

Example of such a code would be:

var vector = new ol.layer.Vector({
  source: source,
  style: style,
  name: 'My layer name'
  • Hmmm, i got it, but I confused a little, if they have not name why I can see their name in getLayers().item(i).get('source') like this tiger:topp part one is datastore name (in GeoServer) second part is layer name
    – Navid
    Apr 24 at 17:56
  • 1
    With getLayers().item(i).get('source') you get source object of your layer, and what you see is value of params option attribute LAYERS: "topp:states" when defining layer source.
    – TomazicM
    Apr 24 at 18:12

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.