Solved! Go to Solution.
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" xmlns:esri="http://www.esri.com/2008/ags" paddingBottom="8" paddingLeft="8" paddingRight="8" paddingTop="8" backgroundColor="0x999999"> <mx:Script> <![CDATA[ import mx.collections.ArrayCollection; import mx.controls.RadioButton; import mx.events.FlexEvent; import mx.events.ItemClickEvent; private function loadLayerName():void { myLegend.layers = null; layerPanel.removeAllChildren(); //loop through each layer and add as a radiobutton for(var i:uint = 0; i < (dynamicLayer.layerInfos.length); i++) { var radioBtn:RadioButton = new RadioButton; radioBtn.groupName = "radioBtnGroup"; radioBtn.value = i; radioBtn.label = dynamicLayer.layerInfos.name; layerPanel.addChild(radioBtn); } //set the visible layer the first radio button radioBtnGroup.selectedValue = 0; dynamicLayer.visibleLayers = new ArrayCollection([0]); myLegend.layers = [dynamicLayer]; myLegend.visible = true; } private function radioClickHandler(event:ItemClickEvent):void { myLegend.layers = null; // update the visible layers to only show the layer selected dynamicLayer.visibleLayers = new ArrayCollection([event.index]); myLegend.layers = [dynamicLayer]; } ]]> </mx:Script> <mx:HDividedBox width="100%" height="100%" backgroundColor="0x999999"> <mx:Canvas height="100%" width="100%" backgroundColor="0xffffff"> <esri:Map id="myMap"> <esri:extent> <esri:Extent xmin="-126" ymin="27" xmax="-72" ymax="50"> <esri:SpatialReference wkid="4326"/> </esri:Extent> </esri:extent> <esri:ArcGISDynamicMapServiceLayer id="dynamicLayer" name="Legend" load="loadLayerName()" url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/{myURL.selectedItem.value}/MapServer" /> </esri:Map> </mx:Canvas> <mx:Canvas height="100%" width="300" backgroundColor="0xffffff" horizontalScrollPolicy="off" verticalScrollPolicy="off"> <mx:VBox width="300" height="100%" paddingLeft="10" paddingTop="10" paddingRight="10" paddingBottom="10" verticalGap="8"> <mx:ComboBox id="myURL" selectedIndex="0" labelField="label" width="100%"> <mx:ArrayCollection> <mx:Object label="US City State Highway" value="Specialty/ESRI_StateCityHighway_USA" /> <mx:Object label="US City State Rivers" value="Specialty/ESRI_StatesCitiesRivers_USA" /> <mx:Object label="US Demographics" value="Demographics/ESRI_Census_USA" /> </mx:ArrayCollection> </mx:ComboBox> <mx:VBox id="layerPanel" width="100%" height="20%" verticalGap="3"> <mx:RadioButtonGroup id="radioBtnGroup" itemClick="radioClickHandler(event)"/> </mx:VBox> <mx:Canvas id="legendPanel" width="100%" height="80%"> <esri:Legend id="myLegend" layers="[]" map="{myMap}" visible="false" respectCurrentMapScale="false"/> </mx:Canvas> </mx:VBox> </mx:Canvas> </mx:HDividedBox> </mx:Application>
import mx.core.FlexGlobals; FlexGlobals.topLevelApplication.myLegend = [layer];
map.getLayer("LayerX").visible = false;<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
xmlns:esri="http://www.esri.com/2008/ags"
>
<mx:Script>
<=!=[=C=D=A=T=A=[
import mx.events.ItemClickEvent;
import mx.collections.ArrayCollection;
import mx.controls.RadioButton;
private function loadLayerName():void
{
layerPanel.removeAllChildren();
//loop through each layer and add as a radiobutton
for(var i:uint = 0; i < (dynamicLayer.layerInfos.length); i++)
{
var radioBtn:RadioButton = new RadioButton;
radioBtn.groupName = "radioBtnGroup";
radioBtn.value = i;
radioBtn.label = dynamicLayer.layerInfos.name;
layerPanel.addChild(radioBtn);
}
//set the visible layer the first radio button
radioBtnGroup.selectedValue = 0;
dynamicLayer.visibleLayers = new ArrayCollection([0]);
}
private function radioClickHandler(event:ItemClickEvent):void
{
// update the visible layers to only show the layer selected
dynamicLayer.visibleLayers = new ArrayCollection([event.index]);
}
]=]=>
</mx:Script>
<esri:Map id="myMap">
<esri:extent>
<esri:Extent xmin="-126" ymin="27" xmax="-72" ymax="50">
<esri:SpatialReference wkid="4326"/>
</esri:Extent>
</esri:extent>
<esri:ArcGISDynamicMapServiceLayer id="dynamicLayer"
load="loadLayerName()"
url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/{myURL.selectedItem.value}/MapServer" />
</esri:Map>
<mx:ComboBox id="myURL" selectedIndex="0" horizontalCenter="0" labelField="label">
<mx:ArrayCollection>
<mx:Object label="US City State Highway" value="Specialty/ESRI_StateCityHighway_USA" />
<mx:Object label="US City State Rivers" value="Specialty/ESRI_StatesCitiesRivers_USA" />
<mx:Object label="US Demographics" value="Demographics/ESRI_Census_USA" />
</mx:ArrayCollection>
</mx:ComboBox>
<mx:Panel id="layerPanel" width="300" height="250" x="20" y="250" paddingLeft="20">
<mx:RadioButtonGroup id="radioBtnGroup" itemClick="radioClickHandler(event)"/>
</mx:Panel>
</mx:Application>