How to obtain latitude and longitude coordinates on mouse click ?

2756
1
06-13-2018 08:27 AM
KevinDunning1
New Contributor

I write code in C# and am currently using the ArcGis Runtime services for UWP. I am trying to obtain the geoposition information from a mouseclick event in my Map control. This is my first time working with the Map SDK and I could use some direction.

0 Kudos
1 Reply
ThadTilton
Esri Contributor

Hey Kevin -

One way to do this is to handle the GeoView.GeoViewTapped event to get a map location, then project the point (if necessary) to get the geographic coordinates (longitude and latitude). Here's an example:

mapView.GeoViewTapped += (s, e) => 
{
    double latitude;
    double longitude;

    // Get the point clicked (it will be in the map's coordinate system, which may not be geographic).
    MapPoint mapPointRaw = e.Location;
    if(mapPointRaw.SpatialReference != SpatialReferences.Wgs84)
    {
        // Project the click point to geographic coordinates.
        MapPoint mapPointLatLong = GeometryEngine.Project(mapPointRaw, SpatialReferences.Wgs84) as MapPoint;
        longitude = mapPointLatLong.X;
        latitude = mapPointLatLong.Y;
    }
    else
    {
        // No need to project, point is already in geographic coordinates.
        longitude = mapPointRaw.X;
        latitude = mapPointRaw.Y;
    }
};‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

You can also use the MapView.SketchEditor to get geometry from the user (not just points, but also lines or polygons, etc). You can make a call like this from a button click event to get a map point. Like the previous example, this point will be in the spatial reference (coordinate system) of the map. You may need to project it to get latitude/longitude coordinates.

MapPoint mapPointRaw = await mapView.SketchEditor.StartAsync(SketchCreationMode.Point) as MapPoint;

 Hope that helps! Thad

PS - here's a sample that illustrates providing tools for the user to sketch geometry on the map that you might find useful: Sketch graphics on the map—ArcGIS Runtime SDK for .NET Samples | ArcGIS for Developers 

0 Kudos