Changing visible legend on LayerGroup selection

903
3
Jump to solution
04-21-2021 02:28 PM
AngusHunt
New Contributor II

Explanation:

I'm currently trying to change the visible legend when a user selects one of three grouped layers to view. These can then be interacted with in a LayerList widget. Only one layer can be selected at a time, so the legend won't clash with any others. I've tried to look up for an event that runs on selecting a different layer via the LayerList, but haven't come across anything yet. Is this possible?

 

Code:

 

var groupLayer = new GroupLayer({
                    title: 'Tauranga Points of Interest',
                    visible: true,
                    visibilityMode: 'exclusive',
                    layers: [LAYERS_ADDED_IN_HERE],
                    opacity: 0.7
                })

 

 

Images:

 Showing the first legend - all normal.Showing the first legend - all normal.

Other legends not showing on layer change. The legend is currently trying to display the same 'Tsunami Evacuation Zones' layer as before.Other legends not showing on layer change. The legend is currently trying to display the same 'Tsunami Evacuation Zones' layer as before.

0 Kudos
1 Solution

Accepted Solutions
UndralBatsukh
Esri Regular Contributor

Hi there, 

It should work. Perhaps you are adding one of layer types that is not supported in the Legend widget? You can see the list of unsupported layers from the sdk doc.

Screen Shot 2021-04-21 at 3.46.55 PM.png


If it is not working, could you please provide a test app? Looks like you probably already know about this sample: https://developers.arcgis.com/javascript/latest/sample-code/widgets-layerlist-actions/

I have updated this sample to use legend and did not see any issues: https://codepen.io/U_B_U/pen/mdRQpwM?editors=1000

 

-Undral

 

 

View solution in original post

3 Replies
UndralBatsukh
Esri Regular Contributor

Hi there, 

It should work. Perhaps you are adding one of layer types that is not supported in the Legend widget? You can see the list of unsupported layers from the sdk doc.

Screen Shot 2021-04-21 at 3.46.55 PM.png


If it is not working, could you please provide a test app? Looks like you probably already know about this sample: https://developers.arcgis.com/javascript/latest/sample-code/widgets-layerlist-actions/

I have updated this sample to use legend and did not see any issues: https://codepen.io/U_B_U/pen/mdRQpwM?editors=1000

 

-Undral

 

 

AngusHunt
New Contributor II

Thank you! I hadn't thought about adding the LayerGroup as the layer for the legend widget, so this was exactly what I was looking for.

0 Kudos
KristianEkenes
Esri Regular Contributor

Hey!

It looks like your Legend is limiting the layers being displayed in one of two ways. The layers that don't have a legend may have the legendEnabled property set to false OR the Legend widget itself has layerInfos set that don't include the other layers. 

Make sure layer.legendEnabled is true for all layers and that the Legend.layerInfos is null. If it's null the Legend widget should handle visibility of layers automatically... Let me know if that wasn't the issue...

 

Kristian

0 Kudos