Change latitude and longitude format

3323
3
06-22-2021 04:35 PM
johnmarker
New Contributor III

Hello,

I'm getting a point from a mouse click on the map. I use that point to create a MapPoint and use screen to location on that point. This allows MapPoint to have x/y properties. When I do this the x and y properties that MapPoint has are the meter measurements. However, I need the measurements of x/y ->lat/long to be in the degree and minutes format with the range of =-90 and =-180 degrees. 

Example:

45º,-94º lat/lon is about -10503190m, 5653794m x/y.

I know there is the CoordinateFormatter to convert to lat/long, but this does not get it with the range of =-90 and =-180. It remains in terms of meters. 

I may have missed something and would love to be pointed in the right direction, but is there any converter out there that will do this for me?

0 Kudos
3 Replies
NathanCastle1
Esri Contributor

It looks like you have a point in the web mercator spatial reference (which is what the Esri basemaps tend to use) and want to get that point in WGS84, which is what GPS and most consumer services use.

CoordinateFormatter should work. There is an example in the Format Coordinates sample: https://developers.arcgis.com/net/wpf/sample-code/format-coordinates/. Can you please share your code so I can understand what might be happening?

Until then, you can use GeometryEngine to project from web mercator to WGS84:

using Esri.ArcGISRuntime.Geometry;
//....
MapPoint inputPoint = new MapPoint(-10503190, 5653794, SpatialReferences.WebMercator);
var outputPoint = (MapPoint)GeometryEngine.Project(inputPoint, SpatialReferences.Wgs84);
System.Diagnostics.Debug.WriteLine($"{outputPoint.Y}, {outputPoint.X}");
// output: 45.20462984270032, -94.35176109011317

 

0 Kudos
johnmarker
New Contributor III

@NathanCastle1  This is just a small snippet example of what I'm doing.  The MapPoint has the format in meters. Another issue I face when using the coordinate Formatter is all the methods either take a string as an argument or return a string. I have a MapPoint which needs converting to a MapPoint with a different format. using coordinateformatter and it returns a string then I loose the ability to access the lat/long properties of the MapPoint. 

        private void MainMapView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Point p = e.GetPosition(MainMapView);
            MapPoint mp = MainMapView.ScreenToLocation(e.GetPosition(MainMapView));

            Trace.WriteLine(mp.X + "  " + mp.Y);
        }

 

0 Kudos
NathanCastle1
Esri Contributor

It sounds like GeometryEngine.Project is the right option for what you're trying to do. CoordinateFormatter is primarily meant for showing coordinates in various different text formats, while you're primarily after the underlying x,y/lat,long values. GeometryEngine.Project will produce a point in the right spatial reference with access to the spatial properties intact.

MapPoint mp = MainMapView.ScreenToLocation(e.GetPosition(MainMapView));
// GeometryEngine.Project returns a Geometry object, but if the input is a MapPoint, the output can be assumed to be a MapPoint
mp = (MapPoint)GeometryEngine.Project(mp, SpatialReferences.Wgs84);
// Note that WGS84 lat/long corresponds to WebMercator Y/X 
Trace.WriteLine(mp.Y + " " + mp.X);
0 Kudos