Layers on/off across different web services

465
5
02-13-2012 06:17 AM
RobertMyers
New Contributor
Is there a way to turn layers on/off across different web services programmatically by buttons or a combobox?

Example:

WebServicesA
|-LayerA 0
|-LayerA 1
WebServiceB
|-LayerB 0
|-LayerB 1
|-LayerB 2

Button1 - Turns off LayerA 1 and LayerB 0. Turns on LayerA 0, LayerB 1 , and LayerB2
Button2 - Turns off LayerA 0, LayerB 1. Turns on LayerA 1 and LayerB 0

Thank you.
Tags (2)
0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus
Robert,

   If you are talking about ArcGISDynamicMapService than you need to look at the visibleLayers property:

http://help.arcgis.com/en/webapi/flex/apiref/com/esri/ags/layers/ArcGISDynamicMapServiceLayer.html


Something like this:

            private function button1_clickHandler(event:MouseEvent):void
            {
                var servA:ArcGISDynamicMapServiceLayer = map.getLayer("serviceA") as ArcGISDynamicMapServiceLayer;
                var servB:ArcGISDynamicMapServiceLayer = map.getLayer("serviceB") as ArcGISDynamicMapServiceLayer;
                servA.visibleLayers = new ArrayCollection([0]);
                servB.visibleLayers = new ArrayCollection([1,2]);
            }
            
            private function button2_clickHandler(event:MouseEvent):void
            {
                var servA:ArcGISDynamicMapServiceLayer = map.getLayer("serviceA") as ArcGISDynamicMapServiceLayer;
                var servB:ArcGISDynamicMapServiceLayer = map.getLayer("serviceB") as ArcGISDynamicMapServiceLayer;
                servA.visibleLayers = new ArrayCollection([1]);
                servB.visibleLayers = new ArrayCollection([0]);
            


Don't forget to click the Mark as answer check and to click the top arrow (promote) as shown below:
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Robert,

   Don't forget the most important part, marking the thread as answered. That is the first (left most) step in the image below:
0 Kudos
RobertMyers
New Contributor
Thank you very much. Always helpful. I was able to hack my way to getting a couple of the buttons working and alternatively, a dropdown list with the same functionality. However, my goal is to add this to the ArcGIS Viewer for Flex with the dropdownlist control in the widget tray. I am not sure how this should be done.  Do I create a new widget or do I modify the HeaderController widget to included the dropdownlist control?

Bob



<?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" minWidth="955" minHeight="600"
      xmlns:esri="http://www.esri.com/2008/ags" height="100%">
<fx:Script>
  <![CDATA[
   import com.esri.ags.Map;
   import com.esri.ags.layers.DynamicMapServiceLayer;
   import com.esri.ags.layers.Layer;
  
   import mx.collections.ArrayCollection;
     
   import spark.events.IndexChangeEvent;

   private function button1_clickHandler(event:MouseEvent):void
   ///Parcels, Sales, Foreclosures - button
    {
    var servA:ArcGISDynamicMapServiceLayer = myMap.getLayer("mapID1") as ArcGISDynamicMapServiceLayer;
    var servB:ArcGISDynamicMapServiceLayer = myMap.getLayer("mapID2") as ArcGISDynamicMapServiceLayer;
    servA.visibleLayers = new ArrayCollection([0]);
    servB.visibleLayers = new ArrayCollection([1,2]);
    }
   private function button2_clickHandler(event:MouseEvent):void
   //Assessment Appeals - button
    {
    var servA:ArcGISDynamicMapServiceLayer = myMap.getLayer("mapID1") as ArcGISDynamicMapServiceLayer;
    var servB:ArcGISDynamicMapServiceLayer = myMap.getLayer("mapID2") as ArcGISDynamicMapServiceLayer;
    servA.visibleLayers = new ArrayCollection();
    servB.visibleLayers = new ArrayCollection([3]);
    }
  
   protected function mapScheme_changeHandler(event:IndexChangeEvent):void
   {
    if (mapScheme.selectedIndex == 0)
    ///Parcels, Sales, Foreclosures - dropdown
    {
     
      var servA:ArcGISDynamicMapServiceLayer = myMap.getLayer("mapID1") as ArcGISDynamicMapServiceLayer;
      var servB:ArcGISDynamicMapServiceLayer = myMap.getLayer("mapID2") as ArcGISDynamicMapServiceLayer;
      servA.visibleLayers = new ArrayCollection([0]);
      servB.visibleLayers = new ArrayCollection([1,2]);
    }
    //Assessment Appeals - dropdown  
    else if (mapScheme.selectedIndex == 1)
    {
     
      var servA:ArcGISDynamicMapServiceLayer = myMap.getLayer("mapID1") as ArcGISDynamicMapServiceLayer;
      var servB:ArcGISDynamicMapServiceLayer = myMap.getLayer("mapID2") as ArcGISDynamicMapServiceLayer;
      servA.visibleLayers = new ArrayCollection();
      servB.visibleLayers = new ArrayCollection([3]);
    }
   }
  ]]>

</fx:Script>
<fx:Declarations></fx:Declarations>

<esri:Map width="100%" height="100%" id="myMap">
  <esri:extent>
   <esri:Extent xmin="13405207.494668775" ymin="375295.187879043" xmax="13420321.02078715" ymax="393586.429745385">
    <esri:SpatialReference wkid="102690"/>
   </esri:Extent>
  </esri:extent>
  <esri:ArcGISDynamicMapServiceLayer
   url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/TaxParcel/IndustryFocusedPublicAccessMap/..."
   id="mapID">

  </esri:ArcGISDynamicMapServiceLayer>
  <esri:ArcGISDynamicMapServiceLayer
   url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/TaxParcel/TaxParcelQuery/MapServer"
   id="mapID1"  alpha="0.5">
   <esri:visibleLayers>
    <s:ArrayCollection source="servA"/>
   </esri:visibleLayers>
  </esri:ArcGISDynamicMapServiceLayer>
 
  <esri:ArcGISDynamicMapServiceLayer
   url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/TaxParcel/AssessorsLiveLayers/MapServer"
   id="mapID2">
   <esri:visibleLayers>
    <s:ArrayCollection source="servB"/>
    </esri:visibleLayers>
  </esri:ArcGISDynamicMapServiceLayer>

</esri:Map>

<s:Button x="22" y="77" label="Parcels, Sales, Foreclosures" click="button1_clickHandler(event)" width="206"/>
<s:Button x="22" y="117" label="Parcels, Assessment Appeals" click="button2_clickHandler(event)" width="206"/>

<s:DropDownList x="22" y="157" id="mapScheme" change="mapScheme_changeHandler(event)" width="206">
  <mx:ArrayCollection>
   <fx:String>Parcels, Sales, Foreclosures</fx:String>
   <fx:String>Assessment Appeals</fx:String>
  </mx:ArrayCollection>
</s:DropDownList>

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

  I would definetly add it to the HeaderControllerWidget.mxml.
0 Kudos
RobertMyers
New Contributor
NOTE: The above "import com.esri.ags.layers.DynamicMapServiceLayer;" should be "import com.esri.ags.layers.ArcGISDynamicMapServiceLayer;"
-----
I modified the HeaderControllerWidget to add the dropdownlist control and added the services to the config file. (both files attached) Seems to be working when all the layers are "on".  But the LayerListWidget and I think it's the MapSwitchWidget with layers under the "More..." button, do not update to reflect the layers being turned on and off by the dropdownlist control selection. So if layers are off they do not come on.

It seems the LayerListWidget and MapSwitchWidget update each other is some way which is what I think I need to do. How can I cause the two widgets to update with the dropdownlist control?

EDIT: I have found a more recent posting to try. http://forums.arcgis.com/threads/20965-LiveMapsWidget-doesn-t-follow-changes

Thanks.
0 Kudos