Solved! Go to Solution.
Try calling refresh() and pass in layerInfos for the layers you want to show.
Does LoadLegend run throughout the life of your app or is RecreateLegend called from other places?
var layer = map.getLayer(layerId); layer.setVisibility(!layer.visible);
Using refresh() might not be the best way to do this. Apologies for sending you down that road.
The legend respects layer visibility so if you're using respectCurrentMapScale: false, and you toggle your layer's visibility, the legend should update correctly.
Instead of trying to manually refresh the legend, can your event handler that fires when your drop-down changes update a layer's visibility? That would look something like:var layer = map.getLayer(layerId); layer.setVisibility(!layer.visible);
So in other words the legend doesn't work on invisible layer period!
In the context of a layer object, layer visibility and the visible scale range for a layer are mutually exclusive. This is not intuitive. We've discussed this internally but have not come up with a cleaner implementation for the API. A layer's visible property can be true (and when it is, it should be included in the legend), but if you're outside the visible scale range for the layer, you do not see it on the map. This is what I was suggesting. If it's not what you're seeing, please contact support and log a bug.