How do you get the map coordinates of a tool's click location

4907
8
03-24-2016 02:07 PM
UmaHarano
Esri Regular Contributor

This question was posted recently by Ted Chapin  @Ted"How do you get the map coordinates of a tool's click location?"

Here is a code snippet to do this:

protected override void OnToolMouseDown(MapViewMouseButtonEventArgs e)

       {

           if (e.ChangedButton == MouseButton.Left)

               e.Handled = true;//Let Framework know we want to handle MouseDown asynchronously

        }

        private MapPoint _lastClickedMapPoint = null;

        protected override async Task HandleKeyDownAsync(MapViewKeyEventArgs k)

       {

           _lastClickedMapPoint = await QueuedTask.Run(() => ActiveMapView.ClientToMap(k.ClientPoint));

           //TODO - do something with _lastClickedMapPoint

       }

Thanks

Uma Harano

ArcGIS Desktop SDK Team 

8 Replies
WoodyHynes
Esri Contributor
0 Kudos
TedChapin
Occasional Contributor III

GeoNet fail! If you have a question, don't ask a question - start a discussion!

0 Kudos
TedChapin
Occasional Contributor III

Having a hard time translating this to VB.  Need to inherit from MapTool, correct?  Can't get the HandleKeyDownAsync function syntax.

0 Kudos
ThomasEmge
Esri Contributor

The templates in Visual Studio are available in C# and VB.NET. Even though most of the examples and snippets are C# VB.NET is fully supported in the VS environment.

0 Kudos
CharlesMacleod
Esri Regular Contributor

Ted, try this (from http://converter.telerik.com/)

Protected Overrides Sub OnToolMouseDown(e As MapViewMouseButtonEventArgs)
   If e.ChangedButton = MouseButton.Left Then
      e.Handled = True
   End If 'Let Framework know we want to handle MouseDown asynchronously
End Sub

Private _lastClickedMapPoint As MapPoint = Nothing

Protected Overrides Function HandleKeyDownAsync(k As MapViewKeyEventArgs) As Task
     _lastClickedMapPoint = Await QueuedTask.Run(Function() ActiveMapView.ClientToMap(k.ClientPoint))
         'TODO - do something with _lastClickedMapPoint
End Function
Oh - I think also you are looking for the starting point - In Visual Studio, add a new project of type Module (within ArcGIS folder). In the project, right-click Add New Item, again in the ArcGIS folder pick MapTool or SketchTool..
TedChapin
Occasional Contributor III

It's making more sense now.  Part of my problem was that when you override a method like HandleKeyDownAsync, Visual Studio autocomplete inserts "Return MyBase.HandleKeyDownAsync(k)". But once you make the function async, the return statement becomes invalid.  It's not needed. Autocomplete is not detecting the async keyword very well.

There's a good sample in the ClientToMap method docs that is exactly what I was looking for:

ArcGIS Pro 1.2 API Reference Guide

0 Kudos
RichardDaniels
Occasional Contributor III

Does this work, or should we be using

HandleMouseDownAsync instated of HandleKeyDownAsync?

protected override async Task HandleMouseDownAsync(MapViewMouseButtonEventArgs e)

{

//Convert the clicked point in client coordinates to the corresponding map coordinates.

mapPoint = await QueuedTask.Run(() => MapView.Active.ClientToMap(e.ClientPoint));

}

What is needed is a code example of the basic process of 'selecting tool from list (e.g., a combobox)', then click on map to get coordinates, then do something with them (e.g., display coord in pop-up). This is not provided as a code snip it in the SDK help documents.

BASED on the new sample published on GeoNet (7/2016) they are using:

 protected override void OnToolMouseDown(MapViewMouseButtonEventArgs e) { .. }

and

protected override Task HandleMouseDownAsync(MapViewMouseButtonEventArgs e) { ... }

which appears to work.

0 Kudos
Wolf
by Esri Regular Contributor
Esri Regular Contributor

Hi Richard,

 This sample might help: https://github.com/Esri/arcgis-pro-sdk-community-samples/blob/master/Map-Exploration/BasicMapTool it is displaying the clicked on coordinates. 

 And this samples has a combo box for the selection of a specific layer: https://github.com/Esri/arcgis-pro-sdk-community-samples/tree/master/Map-Exploration/IdentifyWindow 

Hope this helps.

0 Kudos