Set Layer Visibility in URL

149
3
01-07-2020 09:50 AM
Highlighted
New Contributor II

I have a web map with multiple layers served from my local rest map services. I want to build a web app that will allow me to specify in the url which of those layers are on or off. 

I understand the default visibility is set in the web map. But the visibility can be changed in the web app with the LayerList widget. I want to change the visibility at load time without the user having to open the LayerList. I know there is an entry point to affecting the layer visibility through code because LayerList does it.

I want to pass in my url http://<path>/?layers=1,2,3 so that all layers are turned off except those that match to indexes 1, 2, and 3 in my array of layers. I can take care of a lookup to get the names of these layers. Once I have a reference to the operating layers, I can set the visibility property to true or false for the specified layers. When I step through my code, I can see that the visibility values are changed, but there is no corresponding impact on the map.

I think what I am missing are 1) referencing the operating layers correctly (this.operLayerInfos in LayerList/Widget.js); and 2) calling my function from a proper location where it will be invoked when the map loads. Or maybe visibility isn't the right property?

I would appreciate any guidance for how/where to add this functionality.

First draft code in attachment.

Reply
0 Kudos
3 Replies
Highlighted
MVP Esteemed Contributor

Chris,

   You need to work with the jimu.js classes if you want WAB to recognize that you have changed a layers visibility.

Look at the code in the Layer Toggle Button widget. It uses the Jimu LayerStructure class to get the layer objects in the webmap.

Layer Toggle Button Widget Version 2.12 - 08/28/2019 

Reply
0 Kudos
Highlighted
New Contributor II

Thank you. This looks like a good starting point. I will have to work with it a bit. I will report back when I have progress or ask other questions if I get stuck.

Reply
0 Kudos
Highlighted
New Contributor II

I think I found what I need here: Use URL parameters—Web AppBuilder for ArcGIS | Documentation .

I see I can pass in showLayers=<LayerName1>;<LayerName2>. All I need for a numeric lookup is to add in a lookup table.