Layers on/off programmatically - update layer list in widgets

3611
2
02-16-2012 12:19 PM
RobertMyers
New Contributor
I have tried to hack may way through two older posting to resolve my issue with programmatically turn layers on/off and then updating the LayerListWidget and MapSwitchWidget. I am using API 2.5 and the Sample Flex Viewer.

http://forums.arcgis.com/threads/20965-LiveMapsWidget-doesn-t-follow-changes
http://forums.esri.com/Thread.asp?c=158&f=2421&t=300662#939976

I have modified the HeaderControllerWidget to add a dropdownlist control which turns layers on and off but doesn't update the LayerListWidget and MapSwitchWidget. The attached file contains the config.xml and modified HeaderControllerWidget.mxml files.

The App runs but the same problems exists.

I have modified/added to the TOCItem.as:


[INDENT][INDENT]   
replaced  import com.esri.solutions.flexviewer.AppEvent;  with import com.esri.viewer.AppEvent;
replaced  import com.esri.solutions.flexviewer.SiteContainer; with import com.esri.viewer.ViewerContainer;

public function TocItem(parentItem:TocItem = null)
    {
        _parent = parentItem;
 
  //SiteContainer.addEventListener(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, updateCB);
  //AppEvent.addListener(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, updateCB);

  ViewerContainer.addEventListener(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, updateCB);
    }

private function updateCB(event:AppEvent):void
{
  if(this is TocLayerInfoItem)
  {
   var tli:TocLayerInfoItem = this as TocLayerInfoItem;
  
   if(tli.layerInfo.name == event.data){
    setVisible(_visible ? false : true, true);
   }
  }
}[/INDENT][/INDENT]

I added to the AppEvent.as:


[INDENT][INDENT]public static const PROGRAMATIC_LAYER_VISIBILITY_CHANGED:String = "programicLayerVisibilityChanged";[/INDENT][/INDENT]


And added to the HeaderControllerWidget


[INDENT][INDENT]   private function RefreshLayers(event:Event):void
   {
    //Alert.show("Test")
    ViewerContainer.dispatchEvent(new AppEvent(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED,"TaxParcel"));
    ViewerContainer.dispatchEvent(new AppEvent(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED,"Assessment"));
   }
  
   protected function mapScheme_changeHandler(event:IndexChangeEvent):void
   {
    {
     if (mapScheme.selectedIndex == 0)
      ///Parcels, Sales, Foreclosures - dropdown
     {
      //Alert.show("")
      var servA:ArcGISDynamicMapServiceLayer = map.getLayer("TaxParcel") as ArcGISDynamicMapServiceLayer;
      var servB:ArcGISDynamicMapServiceLayer = map.getLayer("Assessment") as ArcGISDynamicMapServiceLayer;
     
      servA.visibleLayers = new ArrayCollection([0]);
      servB.visibleLayers = new ArrayCollection([1,2]);
      servA.refresh();
      servB.refresh();
      RefreshLayers(event);
     
     }
      //Assessment Appeals - dropdown  
     else if (mapScheme.selectedIndex == 1)
     {
      //Alert.show("")
      var servA:ArcGISDynamicMapServiceLayer = map.getLayer("TaxParcel") as ArcGISDynamicMapServiceLayer;
      var servB:ArcGISDynamicMapServiceLayer = map.getLayer("Assessment") as ArcGISDynamicMapServiceLayer;
      servA.visibleLayers = new ArrayCollection();
      servB.visibleLayers = new ArrayCollection([3]);
      servA.refresh();
      servB.refresh();
      RefreshLayers(event);
     }
    }
   }[/INDENT][/INDENT]

I woudl appreciate any help on this. Thank you.
Tags (2)
0 Kudos
2 Replies
RobertMyers
New Contributor
I managed to hack out some progress on my issue. The LayerListWidget and MapSwitchWidget will "partially" update after the dropdrownlist item selection but only to a point. I need to change the updateCB function to act just like the visiblelayer arraycollections in only turning on those layers in the collection and by default turning the layers not in the array collection off.

It seems the visiblelayer array collections would need to go over to the updateCB function. Then those layers in the array collections would be turned on and the rest turn off.

Any help on this would be appreciated.

Thank you.


-----------------------------------------
I updated the code for the TocItem.as file:
-----------------------------------------
    public function TocItem(parentItem:TocItem = null)
    {
        _parent = parentItem;
 
  //****Updated to ViewContainer***SiteContainer.addEventListener(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, updateCB);
  //****ViewContainer Deprecated***ViewerContainer.addEventListener(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, updateCB);
 
  AppEvent.addListener(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, updateCB);
   
    }

private function updateCB(event:AppEvent):void
{
  if(this is TocLayerInfoItem)
  {
   var tli:TocLayerInfoItem = this as TocLayerInfoItem;
  
   if(tli.layerInfo.name == event.data){
   
    setVisible(_visible ? false : true);
   }
  }
}
----------------------------------------
I updated the HeaderControllerWidget.mxml
----------------------------------------

   protected function mapScheme_changeHandler(event:IndexChangeEvent):void
   {
    {
     if (mapScheme.selectedIndex == 0)
      ///Parcels, Sales, Foreclosures - dropdownlist
     {
     
      var servA:ArcGISDynamicMapServiceLayer = map.getLayer("TaxParcel") as ArcGISDynamicMapServiceLayer;
      var servB:ArcGISDynamicMapServiceLayer = map.getLayer("Assessment") as ArcGISDynamicMapServiceLayer;
     
      servA.visibleLayers = new ArrayCollection([0]);
      servB.visibleLayers = new ArrayCollection([1,2]);
     
      //****Deprecated********ViewerContainer.dispatchEvent(new AppEvent(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, "TaxParcelPublishing"));
             AppEvent.dispatch(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, "TaxParcelPublishing");
      AppEvent.dispatch(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, "Recorded Tax Parcel Sales");
      AppEvent.dispatch(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, "Sheriff's Deeds");
     
     
     
     }
      //Assessment Appeals - dropdown  
            else if (mapScheme.selectedIndex == 1)
     {
     
      var servA:ArcGISDynamicMapServiceLayer = map.getLayer("TaxParcel") as ArcGISDynamicMapServiceLayer;
      var servB:ArcGISDynamicMapServiceLayer = map.getLayer("Assessment") as ArcGISDynamicMapServiceLayer;
     
      servA.visibleLayers = new ArrayCollection();
      servB.visibleLayers = new ArrayCollection([3]);
     
      AppEvent.dispatch(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, "Assessment Appeals");
     
     
     }
    }
   }
0 Kudos
RobertMyers
New Contributor
I was able to get this to work. Although there are certainly some better coding techniques when it comes to the function mapScheme_changeHandler in the HeaderControllerWidget.mxml.

Sample Flex Viewer 2.5
ArcGIS Flex API 2.5
-------------
-------------
Files attached:
config.xml
HeaderControllerWidget.mxml
AppEvent.as
TocItem.as
0 Kudos