Select to view content in your preferred language

Coercion Error with ESRI Flex Legend Component

2012
12
03-02-2011 08:11 AM
GregMcNeill
Emerging Contributor
Hi,
I am in the process of combing the ESRI Dynamic Map Layers on/off Flex Sample and ESRI Legend Component Flex Sample into one application; however, I am getting a #1034: Type Coercion error when I toggle a layer.  When it comes to Flex programming I am a real novice or more of a hack, my background is SQL and VB, so please bear with me.  My interpretation of the error is that I am somehow generating a type miss-match between the data structure used for the Layers list and the structure used for the Legend display.  I thought both of these structures used an array that was populated by Dynamic Service, myDynamicService, therefore, I am very confused as to what is going on here.  I have attached a copy of my source files but here is the code for the Default mxml:
<?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:esri="http://www.esri.com/2008/ags"
      xmlns:samples="com.*"
      pageTitle="Layers and Legends">

 <fx:Script>
  <![CDATA[
   import mx.events.FlexEvent;
   
   protected function application1_initializeHandler(event:FlexEvent):void
   {
    myLegend.layers = [ myDynamicService ];
   }
  ]]>
 </fx:Script>
 
 <s:layout>
  <s:VerticalLayout horizontalAlign="center"
        paddingBottom="20"
        paddingLeft="20"
        paddingRight="20"
        paddingTop="20"/>
 </s:layout>
 
 <mx:HDividedBox width="100%" height="100%">
  <esri:Map id="myMap">
   <esri:ArcGISDynamicMapServiceLayer id="myDynamicService" name=" "
              load="myDynamicService.defaultVisibleLayers()"
              url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Network/USA/MapServer"/>
  </esri:Map>
  <mx:VBox width="40%" height="100%">
   <mx:Panel width="100%" height="50%" borderVisible="true" borderColor="#030303" title="Layers">
    <samples:LayerTOC mapLayer="{myDynamicService}"/>
   </mx:Panel>
   <mx:Panel width="100%" height="50%" borderVisible="true" borderColor="#030303" title="Legend">
    <esri:Legend id="myLegend"
      width="100%" height="100%"
      map="{myMap}"
      respectCurrentMapScale="true"/>
   </mx:Panel>
  </mx:VBox>
 </mx:HDividedBox>
</s:Application>
I hope someone can explain to me what is happening here to generate the Coercion error and hopefully help me resolve it so I can get the Legend to reflect what is displayed on the map.  Any help provided will be greatly appreciated, thank you.

- Greg
Tags (2)
0 Kudos
12 Replies
DasaPaddock
Esri Regular Contributor
What is the full text of the error?

Also, you don't need to set this if you want it to show all layers in the map:
myLegend.layers = [ myDynamicService ];
0 Kudos
GregMcNeill
Emerging Contributor
Dasa ,

          Thank you for the quick reply.  I dropped the myLegend.layers = [ myDynamicService ], it does make sense that I would have to call out the layers since it is already assigned to the Map object.

After I submitted the initial post I noticed that the Legend is being updated in conjunction with the map as the layers are turned on and off once I clear the following error and zoom in or out just a little; panning will not update the layers.

TypeError: Error #1034: Type Coercion failed: cannot convert mx.collections::ArrayCollection@87e04c1 to com.esri.ags.layers.ArcGISDynamicMapServiceLayer.
 at com.esri.ags.skins::LegendSkin/visibleLayersChangeHandler()
 at flash.events::EventDispatcher/dispatchEventFunction()
 at flash.events::EventDispatcher/dispatchEvent()
 at mx.collections::ListCollectionView/dispatchEvent()
 at mx.collections::ListCollectionView/removeItemsFromView()
 at mx.collections::ListCollectionView/listChangeHandler()
 at flash.events::EventDispatcher/dispatchEventFunction()
 at flash.events::EventDispatcher/dispatchEvent()
 at mx.collections::ArrayList/internalDispatchEvent()
 at mx.collections::ArrayList/removeItemAt()
 at mx.collections::ListCollectionView/removeItemAt()
 at com::LayerTOC/hideLayer()
 at com::LayerVizRenderer/cb_clickHandler()
 at com::LayerVizRenderer/__cb_click()



I now see that the application will function as I thought it would as soon as I figure out why I  am getting the above error and figure out how to resolve it from occurring.  Hopefully, the error message is more meaningful to you then it is to me, because I am under the impression I am not creating a type miss-match but obviously I must be wrong. I am open to any suggestions on how to resolve this issue, I appreciate your help.

Thank you,

-Greg
0 Kudos
KenBuja
MVP Esteemed Contributor
Hi Greg,

I've made a project that integrates the TOC component that Tom Hill created for Flex 3 with the Legend component in a Flash Builder 4 project. As each layer is clicked on and offer, its legend is added or removed. As an added bonus, I have also included the code that will identify features in the visible layers and put them into a single InfoWindow, with the selected features of each layer listed in a datagrid on a separate tab.

The forum won't let me upload the project, but the screen shots give you an idea of what it does. Let me know if you'd like to see the project.
0 Kudos
SarthakDatt
Frequent Contributor
Hey Greg,

Thanks for reporting this. Turns out it is a bug in LegendSkin and will be fixed in the next version.

Actually, you can replace the "visibleLayersChangeHandler()" method in the skin you are using with:

private function visibleLayersChangeHandler(event:CollectionEvent):void
{   
     const mapLayers:ArrayCollection = ArrayCollection(hostComponent.map.layers);
     for (var i:int = 0; i < mapLayers.length; )
     {
          if (mapLayers.getItemAt(i) is ArcGISDynamicMapServiceLayer &&
ArcGISDynamicMapServiceLayer(mapLayers.getItemAt(i)).visible &&              ArcGISDynamicMapServiceLayer(mapLayers.getItemAt(i)).visibleLayers === event.target)
          {
                visibleLayersChanged(ArcGISDynamicMapServiceLayer(mapLayers.getItemAt(i)));
                break;
          }
          else
          {
                i++;
          }
     }
}


Hope that helps.
0 Kudos
GregMcNeill
Emerging Contributor
Sarthak,

       Thank you for the information; I have been away working on our migration plan to version 10 and just got back to this today.  I am not sure how to use the code you provided since the function you identified is not in any of the code I am working with, unless it is being brought in from the Reference Library. Again thank you for the information and if you can give me a little more guidance it would be greatly appreciated.

Thanks,

-Greg
0 Kudos
SarthakDatt
Frequent Contributor
Hey Greg,

Apologies for the late response.

This method is part of the LegendSkin class which is shipped out-of-the-box under the skins folder.
Easiest way would be to make a copy of the LegendSkin.mxml and replace the above method.

You can then apply this new skin class to the Legend component:
<esri:Legend id="myLegend"
    width="100%" height="100%"
    map="{myMap}"
   respectCurrentMapScale="true"
                 skinClass="MyLegendSkin"/>



Hope that clarifies things.
0 Kudos
GregMcNeill
Emerging Contributor
Sarthak,

   I appreciate your help. Can you tell me where the LegendSkin.mxml  resides?  Unfortunately I am new to Flex and the skins concept  and have not been able to locate this mxml in the Package Explorer.

Thanks,

Greg
0 Kudos
SarthakDatt
Frequent Contributor
Greg,

When you download the api, the skins folder is part of the zip. All the out of the box skins are in this folder:

arcgis_api_for_flex_2_2 ->ArcGIS_Flex -> skins -> src -> com -> esri -> ags -> skins
0 Kudos
GregMcNeill
Emerging Contributor
Sarthak,

     I got it resolved thanks to your help and Sumedha at ESRI support.  I understand that 2.3 is in testing and close to release which will resolve this issue.  I am very grateful that you took the time to show me the mxl's behind the api. this was very cool.  I am attaching the code just in case anyone else can benefit.

Thanks,

Greg
0 Kudos