Select to view content in your preferred language

KMLLayer getLegendInfos

2382
4
08-02-2011 01:21 PM
RobertScheitlin__GISP
MVP Emeritus
API Team,

I am struggling to to the KMLLayer.getLegendInfos procedure...

Here is what I am trying so far:

var kmllayer:KMLLayer = new KMLLayer("http://explore.data.gov/download/db3a-9tgn/KML");
kmllayer.getLegendInfos(new AsyncResponder(kmlLegendResult,kmlLegendFault,null));

private function kmlLegendResult(lInfos:Array, token:Object):void
        {
            var llInfo:LayerLegendInfo = lInfos[0] as LayerLegendInfo;
            const layName:LegendDataItem = new LegendDataItem();
            layName.lname = llInfo.layerName;
            layName.id = Number.NaN;
            layName.label = llInfo.layerName;
            layName.minscale = llInfo.minScale;
            layName.maxscale = llInfo.maxScale;
            
            const lClass:LegendDataClassItem = new LegendDataClassItem();
            for(var ll2:int =0; ll2 < llInfo.legendItemInfos.length; ll2++){
                var lli:LegendItemInfo = llInfo.legendItemInfos[ll2] as LegendItemInfo;
                lClass.symbolitems.push(processRenderer(lli));
                layName.legendGroup.push(lClass);
            }
            _legendData.addItem(layName);
            FlexGlobals.topLevelApplication.dispatchEvent(new Event("legendDataLoaded$"));
        }
        
        private function kmlLegendFault(evt:FaultEvent):void
        {
            trace(evt.message);
            FlexGlobals.topLevelApplication.dispatchEvent(new Event("legendDataLoaded$"))
        }


legendItemInfos is always 0

Is this something that just does not work yet?
Tags (2)
0 Kudos
4 Replies
SarthakDatt
Frequent Contributor
Robert,

The KMLLayer is different than other layers in that it only sends the request to load itself(kml data) if it is added to the display list(map). The rest of the layers make the initial(map service info) request even if they are not added to the map.

In your case, you can check for the layer load event and see that it is never fired, hence there is no legend info.

Add the KML layer to map and try again. Should work.

Hope that helps.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Sarthak,

  Thanks for the response but I believe there is an issue here is a very simplified test app:

<?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:mx="library://ns.adobe.com/flex/mx" 
               xmlns:ags="com.esri.ags.*"
               xmlns:esri="http://www.esri.com/2008/ags"
               minWidth="955" minHeight="600">
    <fx:Script>
        <![CDATA[
            import com.esri.ags.events.LayerEvent;
            import com.esri.ags.layers.supportClasses.KMLScreenOverlay;
            import com.esri.ags.layers.supportClasses.LayerLegendInfo;
            import com.esri.ags.layers.supportClasses.LegendItemInfo;
            
            import mx.controls.Alert;
            import mx.rpc.AsyncResponder;
            import mx.rpc.events.FaultEvent;
            
            protected function kmllayer1_loadHandler(event:LayerEvent):void
            {
                KMLLayer(event.layer).getLegendInfos(new AsyncResponder(kmlLegendResult,kmlLegendFault,null));
            }
            
            
            private function kmlLegendResult(lInfos:Array, token:Object):void
            {
                var llInfo:LayerLegendInfo = lInfos[0] as LayerLegendInfo;
                Alert.show(llInfo.legendItemInfos.length.toString());
                for(var lii:int =0; lii < llInfo.legendItemInfos.length; lii++){
                    var lli:LegendItemInfo = llInfo.legendItemInfos[lii] as LegendItemInfo;
                    Alert.show( lli.label);
                }
            }
            
            private function kmlLegendFault(evt:FaultEvent):void
            {
                trace(evt.message);
            }
        ]]>
    </fx:Script>
    <ags:Map id="myMap"
             openHandCursorVisible="false">
        <esri:KMLLayer name="KML Layer" alpha="1" visible="true" 
                       url="http://geocommons.com/overlays/119539.kml"
                       load="kmllayer1_loadHandler(event)"/>
    </ags:Map>
</s:Application>
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Sarthak,

  A bit of additional info. When watching the requests in Fiddler there is never any other request made besides the initial request to the utility function on arcgis.com.
0 Kudos
SarthakDatt
Frequent Contributor
Robert,

The KML layer internally creates FeatureLayers(feature collection) and hence you do not see any additional legend request. It uses the renderer information on the featureLayer for creating  legend swatches.

The getLegendInfos() on the KML layer is waiting for these featureLayers to load which in this case is not happening before the responder is called. Have you tried other KML layer(s)?

As a work around for now, you could do something like:
<?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:mx="library://ns.adobe.com/flex/mx" 
               xmlns:ags="com.esri.ags.*"
               xmlns:esri="http://www.esri.com/2008/ags"
               minWidth="955" minHeight="600">
    <fx:Script>
        <![CDATA[
            import com.esri.ags.events.LayerEvent;
            import com.esri.ags.layers.supportClasses.KMLScreenOverlay;
            import com.esri.ags.layers.supportClasses.LayerLegendInfo;
            import com.esri.ags.layers.supportClasses.LegendItemInfo;
            
            import flash.utils.clearTimeout;
            import flash.utils.setTimeout;
            
            import mx.controls.Alert;
            import mx.rpc.AsyncResponder;
            import mx.rpc.events.FaultEvent;
            
            private var timeOutVar:uint;

            protected function kmllayer1_loadHandler(event:LayerEvent):void
            {   
                timeOutVar = setTimeout(getKMLLegend, 100, event.layer); // wait for all feature layers to load
            }            
            
            private function  getKMLLegend(layer:KMLLayer):void
            {   
                clearTimeout(timeOutVar);
                
                layer.getLegendInfos(new AsyncResponder(kmlLegendResult,kmlLegendFault,null));
            }
            
            private function kmlLegendResult(lInfos:Array, token:Object):void
            {   
                var llInfo:LayerLegendInfo = lInfos[0] as LayerLegendInfo;
                Alert.show(llInfo.legendItemInfos.length.toString());
                for(var lii:int =0; lii < llInfo.legendItemInfos.length; lii++){
                    var lli:LegendItemInfo = llInfo.legendItemInfos[lii] as LegendItemInfo;
                    Alert.show( lli.label);
                }
            }
            
            private function kmlLegendFault(evt:FaultEvent):void
            {
                trace(evt.message);
            }
        ]]>
    </fx:Script>
    <ags:Map id="myMap"
             openHandCursorVisible="false">
        <esri:KMLLayer name="KML Layer" alpha="1" visible="true" 
                       url="http://geocommons.com/overlays/119539.kml"
                       load="kmllayer1_loadHandler(event)"/>
    </ags:Map>
    
</s:Application>
0 Kudos