Select to view content in your preferred language

How to use longitude and latitude to set center for map?

3753
6
07-06-2011 06:29 AM
AdrianLeung
New Contributor
Here is my example:
http://maps.google.com/?q=73,-122

I wanna use longitude and latitude to get the map for flex and put a pin at the middle.
Tags (2)
0 Kudos
6 Replies
MLowry
by
Frequent Contributor
Have you tried using the Flex Viewer Extent helper found here:
http://help.arcgis.com/en/webapps/flexviewer/help/01m3/01m300000012000000.htm
0 Kudos
SiqiLi
by Esri Contributor
Esri Contributor
You will need to:
1. Create a mapPoint object based on longitude and latitude values with spatial reference WGS84
2. Project the mapPoint from WGS84 (4326) to to WGS_1984_Web_Mercator_Auxiliary_Sphere (102100), so as to overlay with your other base map layer.
3. Add projected point as a graphic into the graphic layer.

Here is the sample code I wrote for you:

<?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:esri="http://www.esri.com/2008/ags"
      pageTitle="World Street Map">
 
 <fx:Script>
  <![CDATA[
   import com.esri.ags.Graphic;
   import com.esri.ags.SpatialReference;
   import com.esri.ags.events.GeometryServiceEvent;
   import com.esri.ags.events.MapEvent;
   import com.esri.ags.geometry.Geometry;
   import com.esri.ags.geometry.MapPoint;
   import com.esri.ags.symbols.SimpleMarkerSymbol;
   import mx.controls.Alert;
   

   protected function map1_loadHandler(event:MapEvent):void
   {
    //project the lat and long values from 4326 to 102100
    var myPoint:MapPoint = new MapPoint(-122, 73, new SpatialReference(4326));
    geometryService.project([myPoint as Geometry], new SpatialReference(102100),null);
   }

   protected function geometryService_projectCompleteHandler(event:GeometryServiceEvent):void
   {
    var pt:MapPoint = (event.result as Array)[0]as MapPoint;
    var myGraphic:Graphic = new Graphic(pt, new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 22, 0x009933));
    myGraphicsLayer.add(myGraphic);
    map.centerAt(pt);
   }
   
  ]]>
 </fx:Script> 
 
 <fx:Declarations>
  <esri:GeometryService id="geometryService"
         projectComplete="geometryService_projectCompleteHandler(event)"
         url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"/>
 </fx:Declarations>
 
 <esri:Map id ="map" load="map1_loadHandler(event)">
  <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
  <esri:GraphicsLayer id="myGraphicsLayer"></esri:GraphicsLayer>
 </esri:Map>
 
</s:Application>
0 Kudos
SiqiLi
by Esri Contributor
Esri Contributor
Attach a screen shot of the result.
0 Kudos
KenBuja
MVP Esteemed Contributor
You don't need to make a call to a geometry service if you are projecting lat/long to or from Web Mercator. The WebMercatorUtil class does this for you. Here's another version of Shuping's code

<?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:esri="http://www.esri.com/2008/ags"
               pageTitle="World Street Map" xmlns:mx="library://ns.adobe.com/flex/mx">
    
    <fx:Script>
        <![CDATA[
            import com.esri.ags.Graphic;
            import com.esri.ags.SpatialReference;
            import com.esri.ags.events.GeometryServiceEvent;
            import com.esri.ags.events.MapEvent;
            import com.esri.ags.geometry.Geometry;
            import com.esri.ags.geometry.MapPoint;
            import com.esri.ags.symbols.SimpleMarkerSymbol;
            import com.esri.ags.utils.WebMercatorUtil;
            
            import mx.controls.Alert;
            
            
            protected function map1_loadHandler(event:MapEvent):void
            {
                //project the lat and long values from 4326 to 102100
                var myPoint:MapPoint = new MapPoint(-122, 73, new SpatialReference(4326));
//                geometryService.project([myPoint as Geometry], new SpatialReference(102100),null);
                var myGraphic:Graphic = new Graphic(WebMercatorUtil.geographicToWebMercator(myPoint), new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 22, 0x009933));
                myGraphicsLayer.add(myGraphic);
                map.centerAt(myGraphic.geometry as MapPoint);

            }
            
//            protected function geometryService_projectCompleteHandler(event:GeometryServiceEvent):void
//            {
//                var pt:MapPoint = (event.result as Array)[0]as MapPoint;
//                var myGraphic:Graphic = new Graphic(pt, new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_DIAMOND, 22, 0x009933));
//                myGraphicsLayer.add(myGraphic);
//                map.centerAt(pt);
//            }
            
        ]]>
    </fx:Script> 
    
    <fx:Declarations>
        <esri:GeometryService id="geometryService"
                              projectComplete="geometryService_projectCompleteHandler(event)"
                              url="http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"/>
    </fx:Declarations>

        <esri:Map id ="map" load="map1_loadHandler(event)">
            <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
            <esri:GraphicsLayer id="myGraphicsLayer"></esri:GraphicsLayer>
        </esri:Map>    

</s:Application>
0 Kudos
SiqiLi
by Esri Contributor
Esri Contributor
Agree with Ken. 🙂 If just need to project coordinates between 4326 (GCS_WGS_1984) and 102100 (WGS_1984_Web_Mercator_Auxiliary_Sphere) coordinate systems. You can directly use the WebMercatorUtil class without going through the Geometry Service.

http://help.arcgis.com/en/webapi/flex/apiref/com/esri/ags/utils/WebMercatorUtil.html
0 Kudos
AdrianLeung
New Contributor
Thank you very much Ken and Shuping 🙂
Just wondering how much would it cost for licensing ArcGIS for Flex map deploy as a map component into part of a commercial mobile project.
Personally I think ArcGIS is much better than Google Map and others(Yahoo, Bing etc), as in the map quality and usage limitation.
0 Kudos