How to report location in decimal degrees


This sample tool reports the mouse down location in decimal degrees. To convert decimal degrees to DMS (degrees, minutes, seconds), use the LatLonFormat object.

How to use

  1. Load some projected data into the focus map.
  2. Add a new UIToolControl to any toolbar.
  3. Paste the code into VBA.
  4. Make sure the names of the controls match, the sample assumes UIToolControl1.
  5. Completely close VBA so mouse events fire.
  6. Select the tool and then click anywhere on the focus map.
[VBA]
Private Sub UIToolControl1_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
    Dim pMxDoc As IMxDocument
    Dim pPoint As IPoint
    Dim pClone As IClone
    Dim pGeometry As IGeometry
    Dim pSpatialRefFactory As ISpatialReferenceFactory
    Dim pSpatialRef As ISpatialReference
    Dim pGeographicCoordSys As IGeographicCoordinateSystem
    
    'Get the point where the user clicked
    Set pMxDoc = Application.Document
    If pMxDoc.CurrentLocation.IsEmpty Then Exit Sub
    'Clone the point because we don't want to alter
    'the actual document's current location point
    Set pClone = pMxDoc.CurrentLocation
    Set pPoint = pClone.Clone
    Set pGeometry = pPoint 'QI
    
    'Create a new geographic coordinate system to use in the conversion
    Set pSpatialRefFactory = New SpatialReferenceEnvironment
    Set pGeographicCoordSys = pSpatialRefFactory.CreateGeographicCoordinateSystem(esriSRGeoCS_NAD1983)
    Set pSpatialRef = pGeographicCoordSys 'QI
    pSpatialRef.SetFalseOriginAndUnits -180, -90, 1000000
    
    pGeometry.Project pSpatialRef
    MsgBox pPoint.x & ", " & pPoint.y, , "Decimal Degrees"
    
End Sub