Select to view content in your preferred language

Display map at specified location

1683
6
02-28-2012 08:17 PM
MohammedIsmail
Emerging Contributor
hi

i want to display the map at the center of the screen

whenever i load a new map service the map should get displayed at the center of the screen

i tried this but its not working

<esri:Map id="MyML" load="MyML.centerAt(new MapPoint(70.0,6.0))">
<esri:ArcGISDynamicMapServiceLayer id="myDynamicService" name="{myURL.selectedItem}"   
     load="myDynamicService.defaultVisibleLayers()" 
      url="http://gis-server/ArcGis/rest/services/{myURL.selectedItem}/MapServer/" width="100%" height="100%">

</esri:ArcGISDynamicMapServiceLayer>
</esri:Map>


thanks
ISMAIL
Tags (2)
0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Emeritus
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:
0 Kudos
MohammedIsmail
Emerging Contributor
hi

thanks for your kind help

i do have one more problem hope u can help me

as am using dropdownlist box for loading map services of different places like
when i select any service from the list it is getting displayed some where

suppose if load Africa service then i want to load india then ...

am unable to clear the privious coordinate so that i can able to load other service at same place (i.e center of the screen)

    <s:DropDownList id="myURL" selectedIndex="0" creationComplete="myURL_creationCompleteHandler(event)">
        <s:ArrayList>
            <fx:String>India</fx:String>
            <fx:String>Africa</fx:String>
            <fx:String>Austrelia</fx:String>
            <fx:String>USA</fx:String>
              .
              .

        </s:ArrayList>
    </s:DropDownList>


Thanks & Regards
ISMAIL



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:
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Ismail,

   So you would change the myURL_creationCompleteHandler function to this then:

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;
                }
            }


Now your questions have been answered so It is time you mark this thread as answered.

Don't forget to click the Mark as answer check and to click the top arrow (promote) as shown below:
(follow the images directions left to right on the post that answered your question)
0 Kudos
MohammedIsmail
Emerging Contributor
hi Robert

Sorry to disturb you again

its not working after loading first service when i select second its not getting updated with second service

i have searched for refresh, clear option could't find

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 "Raichur":
     {
     MyML.centerAt(new MapPoint(76.90,16.12));
     break;
     }
     case "Yadgir":
     {
     MyML.centerAt(new MapPoint(76.88,16.89));
     break;
        }
       }
   }

Thanks
Ismail
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Ismail,

   This has been tested and works:

<?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>
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Ismail,

   You forgot the do the most important part... Mark the thread as answered. See the image and follow the steps left to right.

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