Select to view content in your preferred language

ArcGISDynamicMapServiceLayer getLegendInfos() not returning legends for SubLayers

1198
6
Jump to solution
02-20-2012 10:58 AM
PremRadhakrishnan
Regular Contributor
I think there is either a bug in the Flex API or I am missing something really small. I am trying to retrieve the legend for a Dynamic Map Service layer but the problem is that I get the legends of all the parent layers and none of the child layers.

For example for something like below where Large Scale and Small Scale are children of Contours and there are two children in each one I only get the legend of the Contours which obviously is nothing . This is multi tier situation but even if there is one child for a parent its still the same result.

Contours
[INDENT]Large Scale[/INDENT]
[INDENT][INDENT]Large Scale 1[/INDENT][/INDENT]
[INDENT][INDENT]Large Scale 2[/INDENT][/INDENT]
[INDENT]Small Scale[/INDENT]
[INDENT][INDENT]Small Scale 1[/INDENT][/INDENT]
[INDENT][INDENT]Small Scale 2[/INDENT][/INDENT]

Thanks in advance.

Cheers
Prem
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
PremRadhakrishnan
Regular Contributor
That makes sense now. I did not realize that for sub layers the LayerLegendInfo is stored within the LayerLegendInfo of the group layer.
Thank you.

View solution in original post

0 Kudos
6 Replies
PremRadhakrishnan
Regular Contributor
I wanted to add some more info to this. While tracking the actual call in Charles (debug proxy) you can see that the REST call returns the proper legends but when you check the result in Flash Builder with a breakpoint  its not the same anymore . So in the debug proxy you can see that the network call returns no parent layers at all and only the layers that have no children in the getLegendInfos() call but when I look at the same result in Flash Builder at a breakpoint it has only the "root" or topmost layers and none of the children.
0 Kudos
SarthakDatt
Frequent Contributor
Hey Premkrishnan,

What version of the API are you using? I just tried getting legends for one of the dynamic map services(has group layers) using the 2.5 API and it works fine:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:esri="http://www.esri.com/2008/ags"
               pageTitle="Legend Component" initialize="application1_initializeHandler(event)">

    <s:layout>
        <s:HorizontalLayout gap="0"/>
    </s:layout>

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;            

            protected function application1_initializeHandler(event:FlexEvent):void
            {
                myLegend.layers = [legendLayer];
               
            }
        ]]>
    </fx:Script>
    
    <esri:Legend id="myLegend"
                 width="250" height="100%"
                 map="{myMap}"
                 respectCurrentMapScale="false"/>
    <esri:Map id="myMap">        
        <esri:ArcGISDynamicMapServiceLayer id="legendLayer" url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer"/>       
    </esri:Map>

</s:Application>


Could you, if its possible share the url to your dynamic map service, so that I can try it at my end as well.

-- Sarthak
0 Kudos
PremRadhakrishnan
Regular Contributor
I am using 2.5 as well but I am not using the Legend component , I am making the getLegendInfos() call on ArcGISDynamicMapServiceLayer with a block of code for use in a custom interface. Unfortunately I cant share the service since all our services are secure.

Here are three screen shots, showing the structure of the map service, the result from the call in Flex , and the result in Charles of the actual call.
Service Structure
[ATTACH=CONFIG]12128[/ATTACH]
Flex Breakpoint Variable
[ATTACH=CONFIG]12129[/ATTACH]
Charles debugging Proxy
[ATTACH=CONFIG]12130[/ATTACH]
0 Kudos
PremRadhakrishnan
Regular Contributor
Ok I just found more information about the Legend Component , tracing the network activity on the Legend sample from the samples section I found that the Legend component uses http://utility.arcgis.com/sharing/tools/legend to generate the legend and not an actual Flex API call to the ArcGISDynamicMapServiceLayer.

Heres is an example of what the legend component calls to generate the legend in this example


http://utility.arcgis.com/sharing/tools/legend?soapUrl=http%3A%2F%2Fsampleserver3%2Earcgisonline%2Ec...

So essentially the API method and the Legend component are two completely different things.
0 Kudos
SarthakDatt
Frequent Contributor
The Legend component internally uses getLegendInfos() only. Also if your server version is less than 10.01, then it uses the http://utility.arcgis.com/sharing/tools/legend utility to retrieve legends, otherwise it uses the direct REST end point on the map service. The response should be the same in both cases.

Here's my earlier sample(not using the Legend component):
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
               xmlns:s="library://ns.adobe.com/flex/spark"
               xmlns:esri="http://www.esri.com/2008/ags">

    <fx:Script>
        <![CDATA[
            import com.esri.ags.events.LayerEvent;
            import com.esri.ags.layers.supportClasses.LayerLegendInfo;
            
            import mx.rpc.AsyncResponder;
            import mx.rpc.Fault;
            
            protected function legendLayer_loadHandler(event:LayerEvent):void
            {
                legendLayer.getLegendInfos(new AsyncResponder(getLegendResult, getLegendFault));
            }
            
            private function getLegendResult(layerLegendInfos:Array, token:Object = null):void
            {   
                for each(var layerLegendInfo:LayerLegendInfo in layerLegendInfos)
                {
                    if (layerLegendInfo.layerLegendInfos) // group layer
                    {   
                        trace(layerLegendInfo);
                        for each(var subLayerLegendInfo:LayerLegendInfo in layerLegendInfo.layerLegendInfos)
                        {
                            trace(subLayerLegendInfo);
                        }
                    }
                    else
                    {
                        // directly look at legenItemInfos
                    }
                }
                
                // trace results(for group layer):
//                (com.esri.ags.layers.supportClasses::LayerLegendInfo)#0
//                layerId = "2"
//                layerLegendInfos = (Array)#1
//                [0] (com.esri.ags.layers.supportClasses::LayerLegendInfo)#2
//                layerId = "3"
//                layerLegendInfos = (null)
//                layerName = "Coarse Counties"
//                layerType = "Feature Layer"
//                legendItemInfos = (Array)#3
//                [0] (com.esri.ags.layers.supportClasses::LegendItemInfo)#4
//                label = ""
//                symbol = (com.esri.ags.symbols::PictureMarkerSymbol)#5
//                angle = 0
//                height = 0
//                source = (flash.utils::ByteArray)#6
//                bytesAvailable = 142
//                endian = "bigEndian"
//                length = 142
//                objectEncoding = 3
//                position = 0
//                width = 0
//                xoffset = 0
//                yoffset = 0
//                values = (null)
//                maxScale = 1000001
//                minScale = 0
//                visible = true
//                [1] (com.esri.ags.layers.supportClasses::LayerLegendInfo)#7
//                layerId = "4"
//                layerLegendInfos = (null)
//                layerName = "Detailed Counties"
//                layerType = "Feature Layer"
//                legendItemInfos = (Array)#8
//                [0] (com.esri.ags.layers.supportClasses::LegendItemInfo)#9
//                label = ""
//                symbol = (com.esri.ags.symbols::PictureMarkerSymbol)#10
//                angle = 0
//                height = 0
//                source = (flash.utils::ByteArray)#11
//                bytesAvailable = 142
//                endian = "bigEndian"
//                length = 142
//                objectEncoding = 3
//                position = 0
//                width = 0
//                xoffset = 0
//                yoffset = 0
//                values = (null)
//                maxScale = 0
//                minScale = 1000000
//                visible = true
//                layerName = "Counties"
//                layerType = "Group Layer"
//                legendItemInfos = (null)
//                maxScale = 0
//                minScale = 1000000
//                visible = true
            }
            
            private function getLegendFault(fault:Fault, token:Object = null):void
            {
                // check for fault
            }            
        ]]>
    </fx:Script>
    
    <esri:Map id="myMap">        
        <esri:ArcGISDynamicMapServiceLayer id="legendLayer" url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer" load="legendLayer_loadHandler(event)"/>
    </esri:Map>
    
</s:Application>


If you look at the response the LayerLegendInfo for grouplayer has its layerLegendInfos property populated with the LayerLegendInfo for the sublayers. You would have to parse the response in a way that supports your custom interface(Legend component does that too internally). For further reference you can look at: http://help.arcgis.com/en/webapi/flex/apiref/com/esri/ags/layers/supportClasses/LayerLegendInfo.html and http://help.arcgis.com/en/webapi/flex/apiref/com/esri/ags/layers/supportClasses/LegendItemInfo.html

I have to agree the documentation is not very helpful right now, will update it to have more information.

Hope that helps.
0 Kudos
PremRadhakrishnan
Regular Contributor
That makes sense now. I did not realize that for sub layers the LayerLegendInfo is stored within the LayerLegendInfo of the group layer.
Thank you.
0 Kudos