Select to view content in your preferred language

Trying to turn off all layers, but get default visibility instead

2350
3
04-10-2013 01:51 PM
DarrenGemoets
Occasional Contributor
I'm using an older version of, well, pretty much everything (ArcGIS API for Flex version 2.0, ArcGIS Viewer for Flex version 2.0 beta 2).  I believe that this post is saying that in older versions of the API like mine, setting visibleLayers to null will set layers to their default visibility.  In com.esri.viewer.components.toc.tocClasses.TocMapLayerItem, the refreshLayer() function calls:
ArcGISDynamicMapServiceLayer(layer).visibleLayers = new ArrayCollection(visLayers);

which presumably means that if visLayers is empty or null, that all layers will be set to their default visibility.  Most of the examples I've seen use visibleLayers to set visibility.

If all of my assumptions are correct, then how do I actually turn off all layers?
If my assumptions are incorrect, then where did I go wrong, and how do I turn off all layers?

Thank you.
Tags (2)
0 Kudos
3 Replies
IvanBespalov
Frequent Contributor
To show all visible by default layers:
dynamicLayer.visibleLayers = null;

To hide all layers set empty collection:
dynamicLayer.visibleLayers = new ArrayCollection();

To show all layers:
dynamicLayer.visibleLayers = new ArrayCollection([array of all layers id's]);;


example:
<?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:HorizontalLayout paddingBottom="10"
       paddingLeft="10"
       paddingRight="10"
       paddingTop="10"/>
 </s:layout>
 
 <fx:Script>
  <![CDATA[
   import com.esri.ags.events.LayerEvent;
   import com.esri.ags.layers.supportClasses.LayerInfo;
   
   import mx.collections.ArrayCollection;
   
   [Bindable]
   private var layersList:ArrayCollection = null;

   [Bindable]
   private var visibleLayers:ArrayCollection = null;
   
   protected function onShowAllClick(event:MouseEvent):void
   {
    visibleLayers = null;
    dlayer.refresh();
   }
   
   protected function onHideAllClick(event:MouseEvent):void
   {
    visibleLayers = new ArrayCollection();
    dlayer.refresh();
   }
   
   protected function onShowSelectedClick(event:MouseEvent):void
   {
    var selectedLayer:LayerInfo = ddlLayers.selectedItem as LayerInfo;
    visibleLayers = new ArrayCollection([selectedLayer.layerId]);
    dlayer.refresh();
   }


   protected function onLayerLoad(event:LayerEvent):void
   {
    layersList = new ArrayCollection(dlayer.layerInfos);
    ddlLayers.selectedIndex = 0;
   }

  ]]>
 </fx:Script>
 
 <s:Panel width="100%"
    height="100%"
    title="Map"> 
  
  <esri:Map id="map">
   <esri:extent>
    <esri:Extent id="lowerManhattan"
        xmin="-8239000" ymin="4968000" xmax="-8235000" ymax="4971000">
     <esri:SpatialReference wkid="102100"/>
    </esri:Extent>
   </esri:extent>
   
   <esri:ArcGISTiledMapServiceLayer name="Base layer - tiled" 
            url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
   
   <esri:ArcGISDynamicMapServiceLayer id="dlayer" 
              name="Demographics layer - dynamic"
              visibleLayers="{visibleLayers}"
              load="onLayerLoad(event)"
              url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer" />
  </esri:Map>
  
 </s:Panel>
 
 <s:Panel width="300"
    height="100%"
    title="Filters">
  
  <s:VGroup paddingBottom="5"
      paddingLeft="5" 
      paddingRight="5"
      paddingTop="5" 
      gap="10">
   
   <s:Button id="btnShowAll" 
       label="Show all layers"
       click="onShowAllClick(event)"/>
   
   <s:DropDownList id="ddlLayers" 
       width="100%"
       dataProvider="{layersList}"
       labelField="name" />
   
   <s:Button id="btnShowSelected" 
       label="Show selected layer"
       click="onShowSelectedClick(event)"/>
   
   <s:Button id="btnHideAll" 
       label="Hide all layers"
       click="onHideAllClick(event)"/>

  </s:VGroup>
  
 </s:Panel>
 
</s:Application>
0 Kudos
DarrenGemoets
Occasional Contributor
Thanks for the reply,  ibespalov.  I'd tried setting it to an empty collection, but for some reason that isn't working in my situation.

In the debugger I can confirm that the TocMapLayerItem is setting visibleLayers to an empty collection.  Then it recurses back up the TocItem.refreshLayer() calls, finishes the processing of the checkbox click, removes an event handler or two, and goes into some deeper code (ImageLoader) to which I do not have source.  When it's all finished, the default layers are again displayed (although the checkboxes are unchecked).
0 Kudos
DasaPaddock
Esri Regular Contributor
Try setting it to an invalid layer id like -1 or 9999.
0 Kudos