DynamicMapLayer visibleLayers name instead of ID?

2066
4
Jump to solution
06-06-2013 12:11 AM
JoeHewitt
New Contributor III
I am populating a combo box with a list of visibleLayers from my ArcGISDynamicMapServiceLayer. However visibleLayers only seems to contain that layers ID number, which is not very user friendly. I was thinking a getDetails or getallDetails might help, but since the visibleLayers is a list of many IDs I'm not sure how identifying all of them would work.

maybe I need a getDetails function that iterates through the array of visibleLayers and returns an array of layer names.
But if there is something more simple  like myDynamicService.visibleLayers.name (haha) then that would be good.

Also why when I add my ArcGISDynamicMapService it appears with a weird name like "ArcGISDynamicMapServiceLayer829" and not the ID name with no function of being able to set a label.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
IvanBespalov
Occasional Contributor III
playing with com.esri.ags.layers.supportClasses.LayerInfo 😉
<?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"       creationComplete="onAppCreated(event)">  <s:layout>   <s:VerticalLayout gap="5"         paddingBottom="10"         paddingLeft="10"         paddingRight="10"         paddingTop="10" />  </s:layout>    <fx:Script>   <![CDATA[    import com.esri.ags.events.LayerEvent;    import com.esri.ags.layers.ArcGISDynamicMapServiceLayer;    import com.esri.ags.layers.supportClasses.LayerInfo;        import mx.collections.ArrayCollection;    import mx.collections.ArrayList;    import mx.collections.IList;    import mx.controls.Alert;    import mx.controls.Label;    import mx.events.FlexEvent;    import mx.utils.StringUtil;        import spark.components.ComboBox;    import spark.components.DataGrid;        protected function onAddLayerClick(event:MouseEvent):void    {     const serviceUrl:String = txtService.text;     const visibleLayers:Array = txtLayers.text.length > 0 ? txtLayers.text.split(",") : null;     const layerName:String = txtName.text;          var dLayer:ArcGISDynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer();     dLayer.url = serviceUrl;     if (visibleLayers && visibleLayers.length > 0)     {      dLayer.visibleLayers = new ArrayList(visibleLayers);     }     if (layerName.length > 0)     {      dLayer.name = layerName;     }     dLayer.addEventListener(LayerEvent.LOAD, onLayerLoad);     dLayer.addEventListener(LayerEvent.LOAD_ERROR, onLayerLoadError);          map.addLayer(dLayer);    }        protected function onLayerLoad(event:LayerEvent):void    {     var lbl1:Label = new Label();     lbl1.text = StringUtil.substitute("Layer '{0}' added", event.layer.name);         infoContainer.addElement(lbl1);          var dLayer:ArcGISDynamicMapServiceLayer = event.layer as ArcGISDynamicMapServiceLayer;     var visibleLayers:IList = dLayer.visibleLayers;     if (visibleLayers)     {      var lbl2:Label = new Label();      lbl2.text = StringUtil.substitute("Defined visible layers ids are: {0}", visibleLayers.toArray().toString());          infoContainer.addElement(lbl2);     }     if (dLayer)     {      var layerInfoGridProvider:ArrayCollection = new ArrayCollection();      var layerInfoComboBoxProvider:ArrayCollection = new ArrayCollection();      for each (var lInfo:LayerInfo in dLayer.layerInfos)      {       // for data grid       var layerInfo:Object = new Object();       layerInfo.Id = lInfo.layerId;       layerInfo.Name = lInfo.name;       layerInfo["Default Visibility"] = lInfo.defaultVisibility;              if (int(lInfo.minScale) == 0 && int(lInfo.minScale) == 0)       {        layerInfo["Visible at scales"] = "Not set";       }       else       {        layerInfo["Visible at scales"] = int(lInfo.minScale) + " - " + int(lInfo.maxScale);       }              if (visibleLayers && visibleLayers.length > 0)       {        layerInfo["In set of visible layers"] = visibleLayers.getItemIndex(lInfo.layerId.toString()) != -1 ? true : false;       }       else       {        layerInfo["In set of visible layers"] = "-";       }              layerInfoGridProvider.addItem(layerInfo);              // for combo box       if (visibleLayers && visibleLayers.getItemIndex(lInfo.layerId.toString()) != -1)       {        layerInfoComboBoxProvider.addItem(lInfo);       }      }            var comboBox:ComboBox = new ComboBox();      comboBox.dataProvider = layerInfoComboBoxProvider;      comboBox.labelField = "name";      infoContainer.addElement(comboBox);            var infoGrid:DataGrid = new DataGrid();      infoGrid.percentWidth = 100;      infoGrid.scroller = null;      infoGrid.dataProvider = layerInfoGridProvider;           infoContainer.addElement(infoGrid);     }    }        protected function onLayerLoadError(event:LayerEvent):void    {     Alert.show(event.fault.faultString, "Error");    }     protected function onAppCreated(event:FlexEvent):void    {     const message:String = "Examples of map services to test are \n" +      "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/MapServer" +      "\nor\n" +      "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/MapServer";          Alert.show(message, "Info");    }    ]]>  </fx:Script>      <s:HGroup width="100%">   <s:Button label="Add Dynamic Layer"       click="onAddLayerClick(event)"/>   <s:TextInput id="txtService"       width="100%"       prompt="servername/arcgis/rest/services/servicename/MapServer"/>   <s:TextInput id="txtLayers"       width="100%"       prompt="comma separated visible layers (not required)"/>   <s:TextInput id="txtName"       width="100%"       prompt="set layer name (not required)"/>  </s:HGroup>    <s:Panel id="panelMap"      title="Map"     width="100%"     height="100%">   <esri:Map id="map"       extentChange="{panelMap.title = 'Map at scale 1:'+map.scale.toFixed()}"/>  </s:Panel>    <s:Panel title="Info"     height="100%"     width="100%">   <s:Scroller width="100%"      height="100%">    <s:VGroup id="infoContainer"         width="100%"        height="100%" />   </s:Scroller>  </s:Panel>   </s:Application>


P.S. ArcGIS api v. 3.3, Flex SDK v. 4.9

View solution in original post

0 Kudos
4 Replies
IvanBespalov
Occasional Contributor III
sample in this thread
0 Kudos
DasaPaddock
Esri Regular Contributor
For each layer id, you can find the corresponding LayerInfo and get the name from there:
http://resources.arcgis.com/en/help/flex-api/apiref/com/esri/ags/layers/ArcGISDynamicMapServiceLayer...
0 Kudos
JoeHewitt
New Contributor III
How would I find the layerInfo of the visibleLayers.

visibleLayers returns an array of numbers, and layerInfos wants to be used a on a layer type.
0 Kudos
IvanBespalov
Occasional Contributor III
playing with com.esri.ags.layers.supportClasses.LayerInfo 😉
<?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"       creationComplete="onAppCreated(event)">  <s:layout>   <s:VerticalLayout gap="5"         paddingBottom="10"         paddingLeft="10"         paddingRight="10"         paddingTop="10" />  </s:layout>    <fx:Script>   <![CDATA[    import com.esri.ags.events.LayerEvent;    import com.esri.ags.layers.ArcGISDynamicMapServiceLayer;    import com.esri.ags.layers.supportClasses.LayerInfo;        import mx.collections.ArrayCollection;    import mx.collections.ArrayList;    import mx.collections.IList;    import mx.controls.Alert;    import mx.controls.Label;    import mx.events.FlexEvent;    import mx.utils.StringUtil;        import spark.components.ComboBox;    import spark.components.DataGrid;        protected function onAddLayerClick(event:MouseEvent):void    {     const serviceUrl:String = txtService.text;     const visibleLayers:Array = txtLayers.text.length > 0 ? txtLayers.text.split(",") : null;     const layerName:String = txtName.text;          var dLayer:ArcGISDynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer();     dLayer.url = serviceUrl;     if (visibleLayers && visibleLayers.length > 0)     {      dLayer.visibleLayers = new ArrayList(visibleLayers);     }     if (layerName.length > 0)     {      dLayer.name = layerName;     }     dLayer.addEventListener(LayerEvent.LOAD, onLayerLoad);     dLayer.addEventListener(LayerEvent.LOAD_ERROR, onLayerLoadError);          map.addLayer(dLayer);    }        protected function onLayerLoad(event:LayerEvent):void    {     var lbl1:Label = new Label();     lbl1.text = StringUtil.substitute("Layer '{0}' added", event.layer.name);         infoContainer.addElement(lbl1);          var dLayer:ArcGISDynamicMapServiceLayer = event.layer as ArcGISDynamicMapServiceLayer;     var visibleLayers:IList = dLayer.visibleLayers;     if (visibleLayers)     {      var lbl2:Label = new Label();      lbl2.text = StringUtil.substitute("Defined visible layers ids are: {0}", visibleLayers.toArray().toString());          infoContainer.addElement(lbl2);     }     if (dLayer)     {      var layerInfoGridProvider:ArrayCollection = new ArrayCollection();      var layerInfoComboBoxProvider:ArrayCollection = new ArrayCollection();      for each (var lInfo:LayerInfo in dLayer.layerInfos)      {       // for data grid       var layerInfo:Object = new Object();       layerInfo.Id = lInfo.layerId;       layerInfo.Name = lInfo.name;       layerInfo["Default Visibility"] = lInfo.defaultVisibility;              if (int(lInfo.minScale) == 0 && int(lInfo.minScale) == 0)       {        layerInfo["Visible at scales"] = "Not set";       }       else       {        layerInfo["Visible at scales"] = int(lInfo.minScale) + " - " + int(lInfo.maxScale);       }              if (visibleLayers && visibleLayers.length > 0)       {        layerInfo["In set of visible layers"] = visibleLayers.getItemIndex(lInfo.layerId.toString()) != -1 ? true : false;       }       else       {        layerInfo["In set of visible layers"] = "-";       }              layerInfoGridProvider.addItem(layerInfo);              // for combo box       if (visibleLayers && visibleLayers.getItemIndex(lInfo.layerId.toString()) != -1)       {        layerInfoComboBoxProvider.addItem(lInfo);       }      }            var comboBox:ComboBox = new ComboBox();      comboBox.dataProvider = layerInfoComboBoxProvider;      comboBox.labelField = "name";      infoContainer.addElement(comboBox);            var infoGrid:DataGrid = new DataGrid();      infoGrid.percentWidth = 100;      infoGrid.scroller = null;      infoGrid.dataProvider = layerInfoGridProvider;           infoContainer.addElement(infoGrid);     }    }        protected function onLayerLoadError(event:LayerEvent):void    {     Alert.show(event.fault.faultString, "Error");    }     protected function onAppCreated(event:FlexEvent):void    {     const message:String = "Examples of map services to test are \n" +      "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Fire/Sheep/MapServer" +      "\nor\n" +      "http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/BloomfieldHillsMichigan/LandusePlanning/MapServer";          Alert.show(message, "Info");    }    ]]>  </fx:Script>      <s:HGroup width="100%">   <s:Button label="Add Dynamic Layer"       click="onAddLayerClick(event)"/>   <s:TextInput id="txtService"       width="100%"       prompt="servername/arcgis/rest/services/servicename/MapServer"/>   <s:TextInput id="txtLayers"       width="100%"       prompt="comma separated visible layers (not required)"/>   <s:TextInput id="txtName"       width="100%"       prompt="set layer name (not required)"/>  </s:HGroup>    <s:Panel id="panelMap"      title="Map"     width="100%"     height="100%">   <esri:Map id="map"       extentChange="{panelMap.title = 'Map at scale 1:'+map.scale.toFixed()}"/>  </s:Panel>    <s:Panel title="Info"     height="100%"     width="100%">   <s:Scroller width="100%"      height="100%">    <s:VGroup id="infoContainer"         width="100%"        height="100%" />   </s:Scroller>  </s:Panel>   </s:Application>


P.S. ArcGIS api v. 3.3, Flex SDK v. 4.9
0 Kudos