Select to view content in your preferred language

legend only displays basemap layers

1050
4
08-13-2012 12:21 PM
deleted-user-ugCMpXci8bn5
Deactivated User
Hello,

I am having trouble getting a Legend to work properly. I have read through the forums and seen similar issues but none that were quite the same as mine... I am using this code below (taken from the API examples).  When I run this as is, the content pane displays only 'No Legend'..but when i comment out the 'layerInfos' line, I get a legend for the basemap only.  I would like a legend for the dynamic layer and not the basemap.
The other commented out lines are from other attempts to make this work.

I am using a single TiledMapServiceLayer for a basemap and a single DynamicMapServiceLayer. I am testing this using Visual Studio in an MVC 3 framework.

Thanks, Jason


        dojo.connect(map, "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: map,
                    //title: 'Legend',
                     layerInfos: layerInfo,
                   // layerInfos: ({layer:ssLayer}),
                  //  layer: ssLayer,
                  // respectCurrentMapScale: false

                }, "legendDiv");
                legendDijit.startup();
            }
        });
        map.addLayers([ssLayer]);
0 Kudos
4 Replies
derekswingley1
Deactivated User
This is similar to the code you've posted but here's a working example with a dynamic legend:  http://jsfiddle.net/wgCJV/

Can you post your code that defines ssLayer?
0 Kudos
deleted-user-ugCMpXci8bn5
Deactivated User
Thanks for fast reply.

The ssLayer is built with just:
ssLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://dotdevgisiis01/ArcGIS/rest/services/SchoolSafety/SchoolSafety/MapServer");


..but my co-worker added this code below to create a check box menu for the sublayers within ssLayer (taken pretty much verbatim from another ESRI example)...If I understand correctly from my readings here, the layers must be visible for the legend class to work? So this is probably the source o fthe problem..if so, how can I create a legend within this system>? This map needs the sublayers to be off at initialization.

function updateLayerVisibility() {

        var inputs = dojo.query(".list_item"), input;

        visible = [];

        for (var i = 0, il = inputs.length; i < il; i++) {
            if (inputs.checked) {
                visible.push(inputs.id);
            }
        }
        //if there aren't any layers visible set the array value to = -1
        if (visible.length === 0) {
            visible.push(-1);


        }

        ssLayer.setVisibleLayers(visible);
}
0 Kudos
deleted-user-ugCMpXci8bn5
Deactivated User
Hello again, I found a workaround by making the legendDigit a global variable and refreshing the legend when the checkbox visibility function is fired.
Although I would still like to have a legend that shows all the layers whether they are visible or not, this will do for now...I do have another question: is there a way I can replace the default 'NoLegend' text when there are no visible layers? I would like to display a custom message there..
thanks, jason
0 Kudos
deleted-user-ugCMpXci8bn5
Deactivated User
: is there a way I can replace the default 'NoLegend' text when there are no visible layers? I would like to display a custom message there..
thanks, jason


in case anyone was wondering:  
esri.bundle.widgets.legend.NLS_noLegend = 'No legend available. <br> Turn on a layer to add to legend.'; 


is how to change the default message for the Legend digit.
0 Kudos