How to convert from cursor coordinates into a spatial reference system


This sample takes the current cursor coordinates and converts them from pixels to map units. It then projects these map coordinates to a projected and geographic spatial reference system, displaying the results in the Status Bar.
The code samples in this section show the fundamentals of programming with ArcObjects. A careful reading of them gives you all the important concepts you need for developing with ArcObjects, as well as an introduction to the most important ArcObjects components.
The code can be typed or copied into the VBA environment in ArcMap or ArcCatalog, after which you can follow through with the VBA debugger.

How to use

  1. Add the code to the MouseMove event of a UIToolControl in ArcMap.
[VBA]
Dim pMxDoc As IMxDocument
Set pMxDoc = ThisDocument

Dim pSpatialRefFactory As ISpatialReferenceFactory
Set pSpatialRefFactory = New SpatialReferenceEnvironment

Dim pProjectedCoodinateSystem As IProjectedCoordinateSystem
Set pProjectedCoodinateSystem = pSpatialRefFactory.CreateProjectedCoordinateSystem(esriSRProjCS_World_Cassini)

Dim pActiveView As IActiveView
Set pActiveView = pMxDoc.FocusMap

Dim pPoint As IPoint
Set pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(x, y)

pPoint.Project pProjectedCoodinateSystem

Dim sMessage As String
sMessage = "Cassini : " & CStr(Round(pPoint.x, 2)) & ", " & CStr(Round(pPoint.y, 2))

Dim pGeographicCoordinateSystem As IGeographicCoordinateSystem
Set pGeographicCoordinateSystem = pSpatialRefFactory.CreateGeographicCoordinateSystem(esriSRGeoCS_WGS1984)

pPoint.Project pGeographicCoordinateSystem
sMessage = sMessage & " and WGS84 : " & CStr(Round(pPoint.x, 2)) & ", " & CStr(Round(pPoint.y, 2))
ThisDocument.Parent.StatusBar.Message(0) = sMessage