var legendResponse = { "layers": [{ "layerId": 0, "layerName": "ROW", "layerType": "Feature Layer", "minScale": 1000000, "maxScale": 0, "legend": [{ "label": "", "url": "393c5a8cc17bc07be57e6aedb7519a27", "imageData": "iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAAAXNSR0IB2cksfwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAENJREFUOI1jYaAyYGFgYGAoWMHwnxqGTYhgYGShhkHIAMXACREMjOQYguxD2rpw1MBRA0cNHB4GUqNcpI0LyS0HsQEAa34JUaZbYooAAAAASUVORK5CYII=", "contentType": "image/png" }] },
var layerLegend = legendResponse.layers[0].legend; legendImgNode = domConstruct.create("img", { src: "data:" + layerLegend.contentType + ";base64," + layerLegend.imageData });
function getLegendJSON() { var divLegend = dojo.byId("legendDiv"); if (divLegend.innerHTML == "") { divLegend.innerHTML = "Creating Legend..."; var url = <<Your_map_Service_Url>> + "/legend"; var requestHandle = esri.request({ "url": url, "content": { "f": "json" }, "callbackParamName": "callback" }); requestHandle.then(requestSucceeded, requestFailed); } } function requestSucceeded(response, io) { var lyr; var htmlString = "<table>"; var divLegend = dojo.byId("legendDiv"); var mapURL = <<Your_map_Service_Url>>; if (response != null && response.layers.length > 0) { for (var iCnt = 0; iCnt < response.layers.length; iCnt++) { lyr = response.layers[iCnt]; if (lyr.legend.length > 1) { htmlString += "<tr><td colspan='2' style='font-weight:bold;'>" + lyr.layerName + "</td></tr>"; for (var jCnt = 0; jCnt < lyr.legend.length; jCnt++) { var src = mapURL + "/" + lyr.layerId + "/images/" + lyr.legend[jCnt].url; var strlbl = lyr.legend[jCnt].label.replace("<Null>", "Null"); htmlString += "<tr><td align='left'><img src=\"" + src + "\" alt ='' /></td><td>" + strlbl + "</td></tr>"; } } else { htmlString += "<tr><td colspan='2' class='tdLayerHeader' style='font-weight:bold;'>" + lyr.layerName + "</td></tr>"; var src = mapURL + "/" + lyr.layerId + "/images/" + lyr.legend[0].url; htmlString += "<tr><td colspan='2' ><img src=\"" + src + "\" alt ='' /></td></tr>"; } } htmlString += "</table>"; } divLegend.innerHTML = htmlString; }
If the layers are version 10 or lower the legend is created using the ArcGIS.com legend service. In order to use the ArcGIS.com legend service your map service needs to be publicly accessible and your application must be able to access ArcGIS.com.