<?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">
<fx:Script>
<![CDATA[
import com.esri.ags.events.LayerEvent;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.layers.ArcGISDynamicMapServiceLayer;
import mx.events.FlexEvent;
protected function myURL_creationCompleteHandler(event:FlexEvent):void
{
var myDynamicService:ArcGISDynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer();
myDynamicService.url = "http://gis-server/ArcGis/rest/services/" + myURL.selectedItem + "/MapServer";
myDynamicService.name = myURL.selectedItem;
MyML.addLayer(myDynamicService);
}
]]>
</fx:Script>
<esri:Map id="MyML" width="100%" height="100%" />
<s:DropDownList id="myURL" selectedIndex="0" creationComplete="myURL_creationCompleteHandler(event)">
<s:ArrayList>
<fx:String>Highway</fx:String>
</s:ArrayList>
</s:DropDownList>
</s:Application>
Ismail,
You can not do it the way you are trying, because the mapservice will likely always get created before the myURL component.
Try this:<?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"> <fx:Script> <![CDATA[ import com.esri.ags.events.LayerEvent; import com.esri.ags.geometry.MapPoint; import com.esri.ags.layers.ArcGISDynamicMapServiceLayer; import mx.events.FlexEvent; protected function myURL_creationCompleteHandler(event:FlexEvent):void { var myDynamicService:ArcGISDynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer(); myDynamicService.url = "http://gis-server/ArcGis/rest/services/" + myURL.selectedItem + "/MapServer"; myDynamicService.name = myURL.selectedItem; MyML.addLayer(myDynamicService); } ]]> </fx:Script> <esri:Map id="MyML" width="100%" height="100%" /> <s:DropDownList id="myURL" selectedIndex="0" creationComplete="myURL_creationCompleteHandler(event)"> <s:ArrayList> <fx:String>Highway</fx:String> </s:ArrayList> </s:DropDownList> </s:Application>
Don't forget to click the Mark as answer check and to click the top arrow (promote) as shown below:
protected function myURL_creationCompleteHandler(event:FlexEvent):void
{
var myDynamicService:ArcGISDynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer();
myDynamicService.url = "http://gis-server/ArcGis/rest/services/" + myURL.selectedItem + "/MapServer";
myDynamicService.name = myURL.selectedItem;
MyML.addLayer(myDynamicService);
switch(myURL.selectedItem){
case "India":{
MyML.centerAt(new MapPoint(70.0,6.0));
break;
}
}
case "Africa":{
MyML.centerAt(new MapPoint(80.0,12.0));
break;
}
}
<?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">
<fx:Script>
<![CDATA[
import com.esri.ags.events.LayerEvent;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.layers.ArcGISDynamicMapServiceLayer;
[Bindable] private var layerCount:int = 0;
protected function myURL_creationCompleteHandler(event:Event):void
{
if(!MyML.getLayer(myURL.selectedItem)){
var myDynamicService:ArcGISDynamicMapServiceLayer = new ArcGISDynamicMapServiceLayer();
myDynamicService.url = "http://gis-server/ArcGis/rest/services/" + myURL.selectedItem + "/MapServer";
myDynamicService.name = myDynamicService.id = myURL.selectedItem;
myDynamicService.addEventListener(LayerEvent.LOAD, setMapCenter);
MyML.addLayer(myDynamicService);
layerCount++;
}else{
setMapCenter(null);
}
}
private function setMapCenter(evt:Event):void
{
switch(myURL.selectedItem)
{
case "Raichur":{
MyML.centerAt(new MapPoint(76.90,16.12));
break;
}
case "Yadgir":{
MyML.centerAt(new MapPoint(76.88,16.89));
break;
}
}
}
]]>
</fx:Script>
<esri:Map id="MyML" width="100%" height="100%" />
<s:DropDownList id="myURL" selectedIndex="0" creationComplete="myURL_creationCompleteHandler(event)" change="myURL_creationCompleteHandler(event)">
<s:ArrayList>
<fx:String>India</fx:String>
<fx:String>Africa</fx:String>
</s:ArrayList>
</s:DropDownList>
<s:Label text="{layerCount}" x="200" y="3" />
</s:Application>