How do I show/hide FeatureLayers programmatically?

01-13-2020 12:44 PM
New Contributor III

I am building a extension of the PrintWidget which will print a specific basemap+featureLayer+other settings. For example, it will have a button which when clicked will print the current map, including whatever the user uploaded with the AddData widget, but using the basemap "streets" and a specific FeatureLayer or two.

In this thread I learned how to programmatically change the basemap. Now I want to add layers to this new basemap (before printing). But, I cannot find a way to do it.

I looked at other threads for possible examples, but they did not work. Namely, this code: 

 var featureLayer = new FeatureLayer(url);;

does not actually show the new layer. The code does add the new layer to the LayerList widget (which I did not want), and it is even checked, but data from the layer does not show up on the map.

What I would like is a way to programmatically "click" (show/hide) on a layer in the LayerList widget, from a different widget. Any ideas?

0 Kudos
2 Replies
MVP Legendary Contributor


   To learn how to toggle a particular layer on or off in WAB, you can take a look at the code in the Layer Toggle Button widget for example code. The main thing is the use of the LayerStructure class and its traversal method to get the layerNode that has a show and hide method.

New Contributor III

Thanks! I'll take a look.

0 Kudos