Select to view content in your preferred language

LiveMapsWidget doesn't follow changes

2612
10
01-12-2011 12:15 AM
GyorgyGyorfi
Deactivated User
Hi!

In Flexviewer 1.3 the  LiveMapsWidget doesn't follow the layer visibility changes if i turn off one or more layers  by actionscript like this:

         public function hideLayer(name:String,service:ArcGISDynamicMapServiceLayer):void
            {
    
      var Arr:Array = service.layerInfos;
    
      for each (var layerInfo:LayerInfo in Arr){
       var idIndex:int =      service.visibleLayers.getItemIndex(layerInfo.id);
       if(layerInfo.name == name)
        service.visibleLayers.removeItemAt(idIndex);
      }
            }    



I tryed to figure out where and how can i refresh the LiveMapsWidget but i realized the bigest problem is that if i modify the layers visibility by code before and then i start the LiveMapsWidget, it shows wrong layer visibility. It seems to read the visibility settings from another property.

please help,
GeorgeG
Tags (2)
0 Kudos
10 Replies
GiosiaPoma
Regular Contributor
It's right because if you want to hide a layer you must set the visibile = false to the item in the livemapwidget. you must use the TOC object and navigate the list of layer in the service. I've made so and it works.
0 Kudos
GyorgyGyorfi
Deactivated User
It's right because if you want to hide a layer you must set the visibile = false to the item in the livemapwidget. you must use the TOC object and navigate the list of layer in the service. I've made so and it works.


I don't clearly understand what should i do. Could you tell me  where did you modify the code?
0 Kudos
GiosiaPoma
Regular Contributor
In your LiveMapsWidget you must use the var levelsTOC:Object = toc.dataProvider and for each TocMapLayerItem navigate each TocLAyerInfoItem and set visible true or false and automatically the TOC content is updatet. It's a little bit complicated but if you manage your layer the widget you must to share the toc.dataprovider between others widget or the main app. I've spent a lot of time over this thing.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
George & Giosia,

   I have code that will change one specific layers visibility at a time by providing the layers name (can be a sublayer also) and update the tocs checkbox all through code. It requires code additions to TocItem.as and AppEvent.as.

It is dispatched with the following line:

SiteContainer.dispatchEvent(new AppEvent(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, false, false, "Major Roads"));


let me know if either of you are interested.
0 Kudos
GiosiaPoma
Regular Contributor
It's a great idea, can you share your code? Pherhaps I use it in future.

Thank you
0 Kudos
GyorgyGyorfi
Deactivated User
in the meantime i found this old thread

http://forums.esri.com/Thread.asp?c=158&f=2421&t=300662#939976

and it's solved my problem, but i extended the code with this at the  LiveMapsWidget.mxml on creationComplete

private function tuc():void
   {
    for each(var layerID:Number in (map.layers[0] as ArcGISDynamicMapServiceLayer).visibleLayers)
    {
     SiteContainer.dispatchEvent(new AppEvent(AppEvent.LAYER_VISIBILITY_CHANGED, false, true, layerID));
     
    }
   }


Now work's fine in any case,

Thanks Robert!
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Guys,

   Here is the code:

Add/Replace these lines in the TocItem.as

public function TocItem( parentItem:TocItem = null )
  {
   _parent = parentItem;
   SiteContainer.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);
    }
   }
  }


Add this to the AppEvent.as

        /**
        * event added by me to listen for a layer being turn off or on in the map
        * event for when a layers visibility has changed programaticly
        */
        public static const PROGRAMATIC_LAYER_VISIBILITY_CHANGED:String = "programicLayerVisibilityChanged";


Then just call it like this:

   private function test1(evt:Event):void
   {
    SiteContainer.dispatchEvent(new AppEvent(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, false, false, "Major Roads"));
   }
0 Kudos
GerardoGarza
Occasional Contributor
For v2.1 of the Viewer, I did the following:

In TocItem.as, I checked layerInfo.id instead of layerInfo.name.

public function TocItem(parentItem:TocItem = null)
{
    _parent = parentItem;
    ViewerContainer.addEventListener(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, updateCheckBox);
}
private function updateCheckBox(event:AppEvent):void
{
 if(this is TocLayerInfoItem)
 {
  var tli:TocLayerInfoItem = this as TocLayerInfoItem;
   
  if(tli.layerInfo.id == event.data){
   setVisible(_visible ? false : true, true);
  }
 }
}


In my calling widget,

private function updateMapSwitcher(newVisibility:Array):void
{
 for each (var id:String in newVisibility)
 {
  // sends event to MapSwitcher widget to turn subLayer on
  ViewerContainer.dispatchEvent(new AppEvent(AppEvent.PROGRAMATIC_LAYER_VISIBILITY_CHANGED, id));
 }
}


AppEvent.as code is the same as Robert's.
0 Kudos
IsharaKotiah
Emerging Contributor
Thanks - that was really helpful Robert and Gerardo!
0 Kudos