HOW TO

Capture map coordinates with a mouse click using ArcObjects SDK for the Microsoft .NET Framework

Last Published: October 28, 2020

Summary

There is no tool that returns the coordinates of a specific location on a map by a mouse click. ArcObjects SDK for the Microsoft .NET Framework can be used to extend the functionality of ArcMap, wherein a new toolbar is created to return the precise coordinate on a map using the Extending ArcObjects functionality, and by modifying the OnMouseDown event.

Image of the sample result

This article provides steps to capture map coordinates with a mouse click in ArcMap using ArcObjects SDK for the Microsoft .NET Framework.

Procedure

  1. Create a custom toolbar using Microsoft Visual Studio with Visual Basic. Refer to the information provided in Walkthrough: Create a custom tool (ArcObjects .NET 10.8 SDK). Follow through the steps up to 'Delete the class1 file'.
  2. Create the toolbar for use in ArcMap. Right-click the project file, and click Add > New Item to open the Add New Item window.
  3. In the Add New Item window, expand ArcGIS > Extending ArcObjects, and select Base Toolbar. Modify the toolbar name as desired, and click Add.
  4. In the same project file, add another new item.
  5. In the Add New Item window, expand ArcGIS > Extending ArcObjects, and select Base Tool.
  6. Name the item as GetCoordinate.vb and click Add.
  7. Select ArcMap Desktop Tool when the ArcGIS New Item Wizard Options window pops up.
  8. When the new item is added, add the following script to the .vb file created in Step 3 under the Public Sub New() class:
Public Sub New()

    'TODO: Define your toolbar here by adding items
    AddItem("<Project_Name>.GetCoordinate")

End Sub
Note:
To change the name of the toolbar to be displayed in ArcMap, edit the Public Overrides ReadOnly Property Caption() method as follows:

Public Overrides ReadOnly Property Caption() As String
        Get
            'TODO: Replace bar caption
            Return "<Toolbar_Name>"
        End Get
End Property
  1. In the GetCoordinate.vb script file, add the following code snippet under the Public Overrides Sub OnMouseDown() method:
Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)

    Dim pMxDoc As IMxDocument = m_application.Document
    Dim pPoint As IPoint = pMxDoc.ActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(X, Y)
    MsgBox("Map X: " & pPoint.X & vbCrLf & "Map Y: " & pPoint.Y)

End Sub
Note:
In some cases, the IPoint variable requires the reference to the Esri.ArcGIS.Geometry library. To add the reference, right-click Reference in the Solution Explorer pane, and click Add Reference. Search for Esri.ArcGIS.Geometry and check the checkbox.
  1. Build the solution by pressing CTRL+Shift+B.
  2. Open ArcMap.
  3. Click Customize > Toolbar. The toolbar created is available in the selection. The tool returns the coordinate of the location on the map by clicking the cursor on the desired point of the map.

Article ID:000024388

Software:
  • AO SDK Microsoft NET Framework 10 8
  • AO SDK Microsoft NET Framework 10 6
  • ArcMap
  • ArcGIS Runtime SDK for NET 10 2 6
  • AO SDK Microsoft NET Framework 10 7
  • ArcGIS Runtime SDK for NET 10 2 7

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options

Related Information

Discover more on this topic