0

I have a scenario with an ArcGIS JS API map where I want to create a legend for a FeatureLayer that is not currently loaded onto the map. Let's say I know layer the endpoint, and I call to get the layer's ILayerDefinition using getLayer from the ESRI REST API. For an example of this, just take a FeatureLayer's url, and add ?f=json to the end. Using esri's FeatureLayer example, you can see what that looks like here:

https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Landscape_Trees/FeatureServer/0?f=json

If you dig into the ILayerDefinition JSON, you can see the drawingInfos property, which has the renderer, and lots of information that seems like it gets used to draw the Legend. However, I cannot find anything in the documentation for Legend that explains how you can build a legend, from scratch, using that ILayerDefinition JSON. The Legend widget seems very tied to the view, and whatever layers are currently on the view. Certain properties seem promising, like layerInfos, activeLayerInfos, or viewModel, but even those seem to require an actual FeatureLayer instance, rather than just the layer.sourceJSON ILayerDefinition object. It seems like you could maybe use the legendElements property of the activeLayerInfos to manually build the Legend widget, but I'm not making the connection between that (or any properties or subproperties of the Legend widget), to the drawingInfo or drawingInfo.renderer properties of the ILayerDefinition.

How can I create a Legend widget using only a layer's sourceJSON from the REST api, without that layer ever having been added to a map?

Edit - ArcGIS's own example

After getting a comment that this is not what the Legend widget is built for, I did some digging in the AGO map viewer. Take the AGO Living Atlas USA Current Wildfires Feature Service as an example. If you open this dataset in the AGO map viewer, you can toggle the layers off. Then, as you click on the name of each layer in the left menu, the right menu opens, and it contains the legend for that layer, even though the layer is not currently visible on the map. (This doesn't seem to be true for the GroupLayer, but it does work for each individual Feature Layer:

enter image description here

This is the behavior I want to achieve. How is this done?

3
  • The Legend widget isn't designed for that. "The legend will only display layers and sublayers that are visible in the view." -- developers.arcgis.com/javascript/latest/api-reference/… You might want to look at alternatives for displaying the symbology of that layer in some other way than the OOTB "Legend" widget. Feb 9 at 19:36
  • @BjornSvensson yes I saw that 😭. The Legend widget does so much work to nicely build a UI for a layer, I thought I could perhaps find a hack that would enable me to hook into the logic that is used within the Legend code, to build a legend from the sourceJSON instead of the layers currently on the map. I see you work for ESRI! Goodonyaz! Other than using the sourceJSON to literally construct my own legend UI, do you have any suggestions for alternatives that don't require a total from-scratch solution? Feb 9 at 19:54
  • @BjornSvensson I edited my question. It appears the behavior I am trying to achieve is actually present in the standard (new) ArcGIS Online Map viewer. In that UI, they appear to be rendering a legend for a layer that is not on the the map. See my edits for details. How are ya'll doing this? If not with a Legend widget, then how? Feb 9 at 21:31

1 Answer 1

1

Sorry, I was wrong in my comment. The widget documentation was a bit misleading. The important info is at https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Legend.html#respectLayerVisibility

// Always displays legend elements for the map's layers
// regardless of their minScale, maxScale, and legendEnabled properties
legend.respectLayerVisibility = false;

You would have to add it to the map, but it doesn't have to be visible.

If you don't want to add the layer to the map, but have the symbols, you might want to look at https://developers.arcgis.com/javascript/latest/api-reference/esri-symbols-support-symbolUtils.html#renderPreviewHTML. See for example https://developers.arcgis.com/javascript/latest/sample-code/visualization-line-patterns/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.