Solved! Go to Solution.
<?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>
<?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>