Select to view content in your preferred language

visibleLayers change issue

789
6
07-23-2010 08:52 AM
RobertScheitlin__GISP
MVP Emeritus
API Team,

   Several times I have asked for an event to added to the API that would fire when the visibleLayers ArrayCollection of a layer has changed and you have always said that I could just listen for the visibleLayers ArrayCollection change event. Well I have tried and tried again and I can not seem to get your suggestion to work... Here is what I an trying:

ArcGISDynamicMapServiceLayer(layer).visibleLayers.addEventListener(CollectionEvent.COLLECTION_CHANGE,updateVisibilty);


Can you assist me in getting your suggestion to work?
Tags (2)
0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Emeritus
bump...                            ...
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Bump...                                                 ....
0 Kudos
DasaPaddock
Esri Regular Contributor
Hi Robert,

Here's a sample that gives me two events. Hope this helps...

<?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">

    <s:layout>
        <s:VerticalLayout horizontalAlign="center"/>
    </s:layout>

    <fx:Script>
        <![CDATA[
            import com.esri.ags.events.LayerEvent;
            
            import mx.events.CollectionEvent;

            protected function dynLyr_loadHandler(event:LayerEvent):void
            {
                dynLyr.visibleLayers.addEventListener(CollectionEvent.COLLECTION_CHANGE, updateVisibilty);
            }
            
            protected function updateVisibilty(event:CollectionEvent):void
            {
                trace(event);
            }

            protected function button1_clickHandler(event:MouseEvent):void
            {
                dynLyr.visibleLayers.removeAll();
                dynLyr.visibleLayers.addItem(5);
            }
        ]]>
    </fx:Script>
    
    <s:Button label="Update Visible Layers" click="button1_clickHandler(event)"/>
    
    <esri:Map id="map">
        <esri:ArcGISDynamicMapServiceLayer id="dynLyr"
                                           load="dynLyr_loadHandler(event)"
                                           url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer"/>
    </esri:Map>

</s:Application>


Trace:

[CollectionEvent kind="reset" location=-1 oldLocation=-1 type="collectionChange" bubbles=false cancelable=false eventPhase=2]
[CollectionEvent kind="add" location=0 oldLocation=-1 type="collectionChange" bubbles=false cancelable=false eventPhase=2]
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Dasa,

   Thanks for the sample...

I have an issue implementing it in the Flex viewer both 2.0 and earlier as the TOC does this instead of changing the visibleLayers arraycollection
ArcGISDynamicMapServiceLayer(layer).visibleLayers = new ArrayCollection(visLayers);

Is there some event that I can use for the arrayCollection being replaced?
0 Kudos
DasaPaddock
Esri Regular Contributor
Since it's a bindable property you can use the ChangeWatcher.watch() method.
Reference:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/binding/utils/ChangeWatcher.ht...

<?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">

    <s:layout>
        <s:VerticalLayout horizontalAlign="center"/>
    </s:layout>

    <fx:Script>
        <![CDATA[
            import mx.binding.utils.ChangeWatcher;
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;

            protected function button1_clickHandler(event:MouseEvent):void
            {
                dynLyr.visibleLayers = new ArrayCollection([ 5 ]);
            }

            protected function dynLyr_initializeHandler(event:FlexEvent):void
            {
                ChangeWatcher.watch(dynLyr, "visibleLayers", updateVisibilty);
            }

            protected function updateVisibilty(event:Event):void
            {
                trace(event);
            }
        ]]>
    </fx:Script>

    <s:Button click="button1_clickHandler(event)" label="Update Visible Layers"/>

    <esri:Map id="map">
        <esri:ArcGISDynamicMapServiceLayer id="dynLyr"
                                           initialize="dynLyr_initializeHandler(event)"
                                           url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer"/>
    </esri:Map>

</s:Application>
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Dasa,

   Your Adobe Knowledge is unmatched... Thanks again.
0 Kudos