I am creating a custom TOC which needs to have both layer toggle and legend item (image data) for the web map. But the web map service does not have Legend Info. Does anybody know how to get legend info from the web map services. Any help would be very appreciated.
If you have created a legend, it should be available right on the map service, something like:
https://raster.nationalmap.gov/arcgis/rest/services/SampleWorldCities/MapServer/legend?f=pjson
You can also create the legend using something like:
Legend | ArcGIS API for JavaScript 3.19
You may find this useful once you get your JSON back from the legend url. [Ripped off from the old 3.x\digit]
/// Key Line
/// src = "data:image/png;base64," + this.rootLayerTOC._legendResponse.layers[this.rootLayer.layerId].legend[0].imageData;///
_getLegendIconUrl: function(legend) { var src = legend.url; if (src == null) { src = "data:image/png;base64," + this.rootLayerTOC._legendResponse.layers[this.rootLayer.layerId].legend[0].imageData; } else if (src != null && src.indexOf('data') == -1) { // in some cases NULL value may cause #legend != #of renderer entry. if (!has('ie') && legend.imageData && legend.imageData.length > 0) { src = "data:image/png;base64," + legend.imageData; } else { if (src.indexOf('http') !== 0) { // resolve relative url src = this.rootLayer.url + '/' + this.serviceLayer.id + '/images/' + src; } if (this.rootLayer.credential && this.rootLayer.credential.token) { src = src + "?token=" + this.rootLayer.credential.token; } else if (esriConfig.defaults.io.alwaysUseProxy) { src = esriConfig.defaults.io.proxyUrl + "?" + src; } } } return src;
Thanks Jordan for the information. But the service I have is a WebMap service which does not have Legend Info JSON. The following is the screenshot of the service. You can see it only has ALL Layers and Tables link, no Legend link
Thanks Chris for the response. The service I have does not have legend Info. Please see following screenshot. I am creating a TOC having both layers toggles and legend image. I was able to create a one for the map service having the Legend Info. But for the ArcGIS Online Map, I couldn't find a way to get the Legend Info.
It's a Feature Service, that's why you are not seeing the legend option, but if you view the properties of each layer you should see the default render in the Drawing Info. You can also see it in the JSON of each layer.
Hi I work with david trying to figure this out. For ArcGIS server maps we are querying the legend information and populating a custom widget with the image data that is returned.
We are looking for something similar for webmaps. I know the renderer is there but how do you just dynamically create something that renders the renderer? First attempts required knowledge and specific code for all possible renderer types.
Do you know of a way to write one set of code that does not require switches based on every possible renderer type?
I'm coming to this late (and in the context of getting a legend from Esri services for a Leaflet map), but I think the discrepancy noted between the questioners and the responders is that the "legend" service URL pattern is only available for services coming from ArcGIS Server (Enterprise) instances, but NOT from ArcGIS Online services. It looks like several questioners were referring specifically to AGOL resources...
Also, not every "FeatureServer" hosted in AGOL (at arcgis.com) will have a corresponding "MapServer" REST endpoint, unlike what you see with a standard ArcGIS Server/Enterprise setup.
I'm really hoping that someone else can confirm if this assessment is correct, and that the "legend" service URL pattern is only available for standard ArcGIS Server REST endpoints (for both Feature or Map service layers) and NOT for ArcGIS Online services (of any service type).
If so, and if I still need to support legends for AGOL feature and map resources (not just for ArcGIS Server/Enterprise resources), how do I generate the legend image for use in a web map interface (perhaps using Leaflet, or some other framework)? We're not planning on using any Dojo/JSAPI components...
I the AGOL REST service endpoints, there is a Drawing Info Renderer JSON object with all the necessary ingredients (symbol type, colors, sizes, etc), but it would be nice not to have to roll my own legend generator for AGOL services...
Yes, Andrew is correct. Feature services from ArcGIS Server web services are created by publishing map services and enabling Feature Access. So if you are using ArcGIS Server, you can change "FeatureServer" in the REST endpoint for the feature service to "MapServer" and get the legend info, however, as Andrew correctly points out, if the feature service is hosted with ArcGIS Online or Portal for ArcGIS, it does not have a corresponding map service because the service was not created via ArcGIS Server - it was published from Portal/AGO. To get the legend for the feature service if there is no map service, you need to use the drawingInfo - see here for an example of the drawingInfo object: http://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire/FeatureServer/1?f=pjson. Also the drawingInfo.renderer does not match the renderer for Esri's JS API (4.8 at the time of writing this), so you will need to use jsonUtils | API Reference | ArcGIS API for JavaScript 4.8 to convert it to a renderer you can use with the JS API. Once you have the drawingInfo, you can convert it to SVG format to render your custom legend - you can use this: symbolPreview | API Reference | ArcGIS API for JavaScript 4.8 . You can test your legend first using Esri's legend widget: Legend | API Reference | ArcGIS API for JavaScript 4.8 - This will show you the legend works even if there is no corresponding map service for the feature service.
I _did_ eventually circle back to this and (with the hints from Rhys) got a working AGOL Feature Service legend image generator for use in a Leaflet map (which also leverages AngularJS). I can now generate legend images for using the drawingInfo.renderer object from the REST endpoint JSON response.
To do this, I had to first use the angular-esri-core library from here: https://github.com/Esri/angular-esri-map
I actually only needed the smaller angular-esri-core.js component to add just the bits of Dojo/JSAPI that I needed (specifically, the esriLoader object) to generate legend images using the JSAPI 4.x symbolPreview object's renderPreviewHTML method. Our version of AngularJS was just under the "supported" line, but the "core" loader library seems to work fine with our older version.
Using the esriLoader object, I loaded these pieces of Dojo/JSAPI:
Once I had an SVG legend image for the drawingInfo.renderer, I also needed to do some minor surgery on the SVG/XML string, to add an "xmlns" tag to make it valid SVG for rendering as a source URL for an img tag.
Fun times!
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.