Select to view content in your preferred language

Trouble with plotting GPS points on the map

2042
3
Jump to solution
09-19-2012 06:45 AM
JasonCantrell
Deactivated User
I'm new to ArcGIS and I'm having some trouble modifying one of the code samples.  I started with MapCoordinates.mxml which is the sample program that displays the coordinates that the mouse pointer is pointing to on the map.  I added a graphics layer and a creationCompleteHandler that would create a graphic with hardcoded coordinates and add it to the graphics layer.  However, my map point keeps displaying in the very center of the map instead of the coordinates that I provide.  

I'll paste my code below and would really appreciate if anybody could tell me what I'm doing wrong.

 <?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="Map Extent and Mouse Coordinates"       creationComplete="application1_creationCompleteHandler(event)">        <fx:Script>         <![CDATA[    import com.esri.ags.events.ExtentEvent;    import com.esri.ags.geometry.Extent;    import com.esri.ags.geometry.MapPoint;    import com.esri.ags.utils.WebMercatorUtil;    import com.esri.ags.Graphic;    import com.esri.ags.SpatialReference;            import mx.events.FlexEvent;              // when mouse (cursor) is on the map ...             private function loadHandler():void             {                 myMap.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);             }              // ... show coordinates of current (mouse) location             private function mouseMoveHandler(event:MouseEvent):void             {                 const mapPoint:MapPoint = myMap.toMapFromStage(event.stageX, event.stageY);                 const latlong:MapPoint = WebMercatorUtil.webMercatorToGeographic(mapPoint) as MapPoint;                 mousecoords.text =                     "x,y is " + mapPoint.x.toFixed(0) + "," + mapPoint.y.toFixed(0)                     + " and Lat/Long is: " + latlong.y.toFixed(6)                     + " / " + latlong.x.toFixed(6);             }              // convert current projected extent to geographic and show as such             protected function showExtentInGeographic(extent:Extent):String             {                 const geoExtent:Extent = WebMercatorUtil.webMercatorToGeographic(myMap.extent) as Extent;                 // return geoExtent.toString() + ".." ;                 return " " + geoExtent.xmin.toFixed(6)                     + ", " + geoExtent.ymin.toFixed(6)                     + ", " + geoExtent.xmax.toFixed(6)                     + ", " + geoExtent.ymax.toFixed(6)                     + "   (wkid: " + geoExtent.spatialReference.wkid + ")";             }        protected function application1_creationCompleteHandler(event:FlexEvent):void    {     //    this.startExtent = new Extent(-82.92, 33.68, -71.67, 40.93,new SpatialReference());     //    this.mapWMS.map.initialExtent = this.startExtent;     //    this.mapWMS.map.zoomToInitialExtent();          var graphic1:Graphic = new Graphic();          var sr1:SpatialReference = new SpatialReference(4326);     var mappoint1:MapPoint = new MapPoint(55.773261,37.545827,sr1);              graphic1.geometry = mappoint1;          myGraphicsLayer.add(graphic1);         }         ]]>     </fx:Script>    <fx:Declarations>   <esri:SimpleMarkerSymbol id="defaultSymbol"          alpha="0.5"          color="0x0000FF"          size="13"          style="circle">    <esri:SimpleLineSymbol/>   </esri:SimpleMarkerSymbol>  </fx:Declarations>      <s:layout>         <s:VerticalLayout paddingTop="6"/>     </s:layout>      <s:HGroup>         <s:Label fontWeight="bold" text="Current map extent:"/>         <s:RichEditableText editable="false" text='xmin="{myMap.extent.xmin.toFixed(0)}" ymin="{myMap.extent.ymin.toFixed(0)}" xmax="{myMap.extent.xmax.toFixed(0)}" ymax="{myMap.extent.ymax.toFixed(0)}"   (wkid="{myMap.spatialReference.wkid}")'/>     </s:HGroup>     <s:HGroup>         <s:Label fontWeight="bold" text="Current map extent (in geographic):"/>         <s:RichEditableText editable="false" text="{showExtentInGeographic(myMap.extent)}"/>     </s:HGroup>     <s:HGroup>         <s:Label fontWeight="bold" text="Current Mouse Coordinates:"/>         <s:RichEditableText id="mousecoords"                             editable="false"                             text="Move the mouse over the map to see its current coordinates..."/>     </s:HGroup>     <s:HGroup>         <s:Label fontWeight="bold" text="Current map scale is"/>         <s:RichEditableText editable="false" text="1:{myMap.scale.toFixed(0)} (level {myMap.level})"/>     </s:HGroup>      <esri:Map id="myMap" load="loadHandler()">         <esri:extent>             <esri:Extent xmin="3035000" ymin="4305000" xmax="3475000" ymax="10125000">                 <esri:SpatialReference wkid="102100"/>                 <!-- same as tiled map service below -->             </esri:Extent>         </esri:extent>         <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>   <esri:GraphicsLayer id="myGraphicsLayer" symbol="{defaultSymbol}"/>     </esri:Map>  </s:Application> 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor
You're adding the map point in a geographic projection to a Web Mercator projection. You need to project its geometry to add it to the correct place

            protected function application1_creationCompleteHandler(event:FlexEvent):void             {                 //                this.startExtent = new Extent(-82.92, 33.68, -71.67, 40.93,new SpatialReference());                 //                this.mapWMS.map.initialExtent = this.startExtent;                 //                this.mapWMS.map.zoomToInitialExtent();                                  var graphic1:Graphic = new Graphic();                                  var sr1:SpatialReference = new SpatialReference(3857);                 var mappoint1:MapPoint = new MapPoint(55.773261,37.545827,sr1);                                  var mappoint2:Geometry = WebMercatorUtil.geographicToWebMercator(mappoint1);                 graphic1.geometry = mappoint2;                                  myGraphicsLayer.add(graphic1);                              } 


And is this location for Moscow? If so, your lat/long is switched.

View solution in original post

0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor
You're adding the map point in a geographic projection to a Web Mercator projection. You need to project its geometry to add it to the correct place

            protected function application1_creationCompleteHandler(event:FlexEvent):void             {                 //                this.startExtent = new Extent(-82.92, 33.68, -71.67, 40.93,new SpatialReference());                 //                this.mapWMS.map.initialExtent = this.startExtent;                 //                this.mapWMS.map.zoomToInitialExtent();                                  var graphic1:Graphic = new Graphic();                                  var sr1:SpatialReference = new SpatialReference(3857);                 var mappoint1:MapPoint = new MapPoint(55.773261,37.545827,sr1);                                  var mappoint2:Geometry = WebMercatorUtil.geographicToWebMercator(mappoint1);                 graphic1.geometry = mappoint2;                                  myGraphicsLayer.add(graphic1);                              } 


And is this location for Moscow? If so, your lat/long is switched.
0 Kudos
JasonCantrell
Deactivated User
You're adding the map point in a geographic projection to a Web Mercator projection. You need to project its geometry to add it to the correct place

And is this location for Moscow? If so, your lat/long is switched.


That did it.  Thanks!  And yes, those were coordinates for Moscow.  I realized my mistake after I added the code you suggested.
0 Kudos
KenBuja
MVP Esteemed Contributor
Glad to help. Please click the check mark to show the question was answered.
0 Kudos