5

I have a map template that I use to display a variety of feature layers and dynamic layers.

I have found a snippet of code to add the legend to my map:

//add the legend
dojo.connect(myMap, 'onLayersAddResult', function (results) {
    var layerInfo = dojo.map(results, function (layer, index) {
        return { layer: layer.layer, title: layer.layer.name };
    });

    if (layerInfo.length > 0) {
        var legendDijit = new esri.dijit.Legend({
            map: myMap,
            layerInfos: layerInfo},
            "legendDiv");
            legendDijit.startup();
        }
    });

The issue is, I can't figure out how to configure the names of the layers, labels, descriptions, etc. Most of the layers in my service are symbolized by Category, so, I want to be able to display those category names in the Legend.

This is what I want to show..or something like it, but with a category or layer heading

This is what shows up. No heading, just the categorized layer without a title or anything.

Any help would be appreciated.

Thanks

2 Answers 2

5

I would suggest using the Table of Contents/Legend Widget for JavaScript API. It has all the functionality you describe and is a very well written, free, widget. I use it in my apps and it is very easy to configure.

1
  • I will definitely give this a try. Thank you, Craig. Aug 19, 2013 at 14:43
4
return { layer: layer.layer, title: layer.layer.name };

the property you are interested in setting is the title and the code you are using currently loops through each individual layer that has been successfully added to set this property using layer.name.

can you confirm that this actually present in the service? what do you see when you set a breakpoint within that loop?

7
  • i should add that this is only true for FeatureLayers. if you are having trouble with an ArcGISDynamicMapService layer, the approach to solve the problem would be to update the service itself so that server/arcgis/rest/service/myservice/MapServer/Legend displays what you want to see, as that is the resource used by the widget to display symbols. see the description in the api reference of the class for more info [link]developers.arcgis.com/en/javascript/jsapi/legend-amd.html Aug 17, 2013 at 4:18
  • Thanks, John. I added a breakpoint as you suggested within the loop and yes, the layer.layer.name property has the name of the layer. It just doesn't seem to display in the legend div. Aug 19, 2013 at 14:43
  • 1
    is your service public? either way, can you post a fiddle showing the problem? Aug 19, 2013 at 15:41
  • please take a look at this screenshot when you get a moment. i.imgur.com/fVn7OTT.png The watchlist on the left shows that title field is indeed getting populated. I can't for the life of me figure out why that portion of text won't show up in the legend itself. Could it be the CSS? Aug 19, 2013 at 19:50
  • 1
    i guess its possible. i used the inspector in Chrome and was reminded that the relevant DOM node is the .esriLegendService label. dropbox.com/s/1u72xl78c3ly8ny/esriLegendServiceLabel.png Using similar tools you might try inspecting to see if the string you need is present and that no CSS rules are preventing it from being displayed. Aug 21, 2013 at 22:10

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.