Reordering GraphicsLayers

01-30-2020 10:27 AM
New Contributor II

I'm using the the JavaScript API v4.14. I have a stack of GraphicsLayers on my map that can be toggled on and off by the user (I'm just using map.add() and map.remove() to toggle). One layer in particular should always be positioned below the others. However, if it's toggled off and back on again, it gets positioned above the other layers. I have been able get around this by removing all the layers during the toggle and re-adding them to ensure they go in the right order. However, it seems awkward to have to this every time a user performs a toggle function as I have a lot of layers. Is there some way to ensure that this particular layer is always below the others without having to figure our which layers are being displayed and then sorting them appropriately? I've tried using the map.reorder() method without any luck.

0 Kudos
1 Reply
Esri Contributor

Have you considered using the LayerList widget? This provides a list of the layers in the map and the ability to switch on/off the visibility of each layer. The position of layers in the list determines their position in the map, and if one layer is "toggled off", it will maintain its position in the map when toggled back on. 

Here's a couple of samples that use the LayerList widget: 

Hope this helps!

0 Kudos