Access Layer Properties for WebMap Layers

594
2
09-10-2022 03:33 PM
StefanFreelan
New Contributor II

Hi All,

I've got a working webmap (JS 4.24), using an AGOL web map but I can't figure out how to access the individual layers contained in the webmap. I just want to be able to reference a specific layer so I can do things like toggle it's visibility.

I've tried a couple of methods (see below), but if they are working I haven't been able to find a way to then reference the layer (where do I put the visible: true, statement? Or the listMode: 'hide' statement?

Obviously I'm fairly new to JavaScript...

Is there a better way to reference a layer in a webmap?

Having referenced it, how do I set it's properties?

Any advice or suggestions appreciated,

thanks,

~stefan

 

Stuff I've tried:

 

const webmap = new WebMap({
portalItem: {
id: "9c161033f604436193805a7484feab3d" // Web Map for JavaScript
}

});

const view = new MapView({
container: "viewDiv",
map: webmap,
});

// get layers when the view is initialized
webmap.when(() => {
// layer 0
const layer0 = webmap.layers.items[0];
});

// OR Find a layer by ID...
const layer1 = webmap.findLayerById("958f6534812a4407925a2a18a640b841");

Tags (3)
0 Kudos
2 Replies
UndralBatsukh
Esri Regular Contributor

Hi there, 

You can loop through all operational layers in your web map after all resources are laded and find the layer you need.  The following code should get you started: 

webmap.loadAll().then(()=>{
  // loop through all operational layers in your webmap
  webmap.layers.forEach((layer)=>{
    console.log("layer.id", layer.id, layer);
    // check your layer here
  });
 });

 

This sample also shows how to get a layer at the specified index. https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=featureeffect-drop-shado...

 

0 Kudos
StefanFreelanFreelan
New Contributor III

Thanks UndralBatsukh, I'll see if I can get this to work.

0 Kudos