I have put together a widget designed to allow users to add/remove layers at runtime either one-by-one or as a group of related layers, in a simple, user-friendly interface.
Use case it was designed for:
I am building a map to be a central data repository for a city government. Users usually only want to see data from their department, but may occasionally need access to other data. There is far too much data visually/performance wise to have it all loaded at a single time. Many users have little to no GIS or computer skills.
Problems in current widgets this widget is designed to compensate for:
Add Data Widget - Not very user-friendly, does not support adding groups of data at once, does not allow limiting data to curated list (Looks like this feature is coming very soon.)
Layers are usually given random ids.
Map Layers Widget- Users can make layers visible or hidden, but not totally add or remove them which could cause performance issues and an overly cluttered layer list.
Screenshots:
Widget Collapsed
Select layers by group
Select layers Individually
How to use:
Open /src/components/layers.ts and replace the dummy data with the layers and groups you want to use. I recommend setting your Map Widget to a WebMap with only a basemap and no layer data and loading all of your data from this widget. By default, groups[0] will appear selected on load, you may wish to set this to null or a different group. If you chose to have a group selected on load, each layer in that group should be set to activeOnLoad: true. By default, layers are loaded as MapImageLayers to support sublayers. You may wish to change the type to a FeatureLayer or some other type of layer.
Possible Improvements (I'm not planning to do additional support or improvements.):
Getting the layers.ts data to load from config.json, so it could be edited without the developer edition.
Adding additional properties to the layers and modify the addLayers function to construct the layers differently based on these properties. (Such as a property that says if a layer should be a FeatureLayer or a MapImageLayer.)
...And if you are interested in these improvements and more, try the Add/Remove Layers 3.0 version.
GIS Developer
City of Arlington, Texas