"No Legend" appears in Legend some ArcGISDynamicMapServiceLayers

1217
16
Jump to solution
05-23-2012 07:32 AM
GregoryDillon
New Contributor III
I'm having an issue with some ArcGISDynamicMapServiceLayers displaying "No Legend" in the legend when there should be one.  

Here is the code for one of the layers I'm having a problem with:

WaterLayer = new esri.layers.ArcGISDynamicMapServiceLayer("http://pri-img-rep-web.abcwua.org/ArcGIS/rest/services/WUAGISWater/MapServer");
    WaterLayer.id = "Water";
    map.addLayer(WaterLayer);
    AddLayerToRegistryPendList(WaterLayer);

Here is the code for displaying the legend for this layer:

var LegDiv = dojo.byId("LegendDiv");

      LegendControl = new esri.dijit.Legend({
        map: map,
        layerInfos: [{
            layer: map.getLayer("Water"),
            title: " "
        }]
    }, LegDiv);

    LegendControl.startup();


Attached is a image of the raw dump of the legend in the service.


Help!   What is wrong???  Is there a setting I need to adjust in the service.
0 Kudos
16 Replies
GregoryDillon
New Contributor III
Try calling refresh() and pass in layerInfos for the layers you want to show.


Yes this is what I'm already doing see code below - What else can I do?:

function LoadLegend() {
    var ddl = document.getElementById(ddlLegendLayerCat);
    ddlLegendLayerCatSelectIndex = ddl.selectedIndex;
    var CatName = ddl.options[ddl.selectedIndex].value.toString();
  
    RecreateLegend(CatName)
}


function RecreateLegend(CatName) {
    var LegDiv = dojo.byId("LegendDiv");

    if (LegendControl == null) {
        LegendControl = new esri.dijit.Legend({
            map: map,
            layerInfos: [{
                layer: map.getLayer(CatName),
                title: " "
            }]
        , respectCurrentMapScale: false
        }, LegDiv);
        LegendControl.startup();
    } else {
        var Layer = map.getLayer(CatName);
        LegendControl.refresh([{ layer: Layer, title: ' '}]);
    }

}
0 Kudos
derekswingley1
Frequent Contributor
Does LoadLegend run throughout the life of your app or is RecreateLegend called from other places?
0 Kudos
GregoryDillon
New Contributor III
Does LoadLegend run throughout the life of your app or is RecreateLegend called from other places?


LoadLegend is called when the drop drown list (ie. select option) is changed.   The drop down list contain the category name which are the "id" on each dynamic layer.
0 Kudos
derekswingley1
Frequent Contributor
Using refresh() might not be the best way to do this. Apologies for sending you down that road.

The legend respects layer visibility so if you're using respectCurrentMapScale: false, and you toggle your layer's visibility, the legend should update correctly.

Instead of trying to manually refresh the legend, can your event handler that fires when your drop-down changes update a layer's visibility? That would look something like:
var layer = map.getLayer(layerId);
layer.setVisibility(!layer.visible);
0 Kudos
GregoryDillon
New Contributor III
Using refresh() might not be the best way to do this. Apologies for sending you down that road.

The legend respects layer visibility so if you're using respectCurrentMapScale: false, and you toggle your layer's visibility, the legend should update correctly.

Instead of trying to manually refresh the legend, can your event handler that fires when your drop-down changes update a layer's visibility? That would look something like:
var layer = map.getLayer(layerId);
layer.setVisibility(!layer.visible);



So in other words the legend doesn't work on invisible layer period!  

I ended up creating a temporary layer setting the visibility for the layers I wanted on the legend, adding it to the map, pointing the legend to it and then doing a refresh and then deleting the layer from the map.   Pretty kluggy!
0 Kudos
derekswingley1
Frequent Contributor
So in other words the legend doesn't work on invisible layer period!


In the context of a layer object, layer visibility and the visible scale range for a layer are mutually exclusive. This is not intuitive. We've discussed this internally but have not come up with a cleaner implementation for the API. A layer's visible property can be true (and when it is, it should be included in the legend), but if you're outside the visible scale range for the layer, you do not see it on the map. This is what I was suggesting. If it's not what you're seeing, please contact support and log a bug.
0 Kudos
GregoryDillon
New Contributor III
In the context of a layer object, layer visibility and the visible scale range for a layer are mutually exclusive. This is not intuitive. We've discussed this internally but have not come up with a cleaner implementation for the API. A layer's visible property can be true (and when it is, it should be included in the legend), but if you're outside the visible scale range for the layer, you do not see it on the map. This is what I was suggesting. If it's not what you're seeing, please contact support and log a bug.


I think the respectCurrentMapScale flag makes sense.   It seems to me the Legend just needs another option to allow it to show layers that are not visible regardless of scale.   In other just the same way the service call to the legend works (http://.../Legend).  

As an example of such a need:

My app is designed for mobile.    I am trying very hard not to display anything like a info window on the map.    This often is annoying to the user and means limited space to touch things.    For that reason when the user selects the legend icon the map disappears and goes to a dedicated screen just for the legend.   I wanted the legend to show every layer the user could turn on in the map.   They select the layer name (this used to be category or dynamic service ID, but the spec changed at the end of this post) and legend is then focus on that layer whether it is turned on or not.    My fix was to add a temporary layer using the url from a layer already retrieved from the map which I look up by the layer name (not the dynamic service's name but the actual name of the layer - ie via layerinfos).   I think had get the layer id and add to the temporary layer's visiblelayers to flip it on.   If the map had been visible (for my app its not) the user would have seen the layer added to the map (really bad).    If the legend just had this new flag I would have just needed to turn it on.
0 Kudos