0

I have a map where i'm loading several WMS layers from different urls. I'm trying to figure out a way to turn each layer on/off by an identifier, but i can't figure out how to add a unique identifier to the layer, or source of the layer. Basically i have:

for (const entry of entries) {
    const tileWMSOptions = {
              url: entry.URL,
              params: { LAYERS: layerNames, TILED: true },  
            };
            const layer = new TileLayer({
              source: new TileWMS(tileWMSOptions),
            });
            mapObj.addLayer(layer);
}

my 'entry' object has an ID that i would like to associate with this layer, so that i can do some sort of

map.getLayers();

run through them, and do something specific if the id of that layer matches what i'm looking for. I don't see any way of doing this from https://openlayers.org/en/latest/apidoc/module-ol_layer_Tile-TileLayer.html or https://openlayers.org/en/latest/apidoc/module-ol_source_TileWMS.html . Any ideas?

1

You can include your own properties in the layer (similar to title in the documentation http://openlayers.org/en/latest/apidoc/module-ol_layer_Tile-TileLayer.html )

layer = new TileLayer({
              source: new TileWMS(tileWMSOptions),
              guid: guid
            });

and retrieve using .get

map.getLayers().getArray()[0].get('guid');
  • Thanks, dumb mistake. Forget i'm working in javascript sometimes! – Phil Sep 22 '18 at 22:00

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.