Monday, May 18, 2009

Display a mouse location in decimal degrees


This sample allows the user to view the mouse location in decimal degrees when the user clicks with the mouse.In this sample, you will be adding a new tool to the interface and writing the code for the tool.


Start ArcMap.
Open an existing map document (.mxd) or add layers to the empty (Untitled) map document. The data added to the map must be projected.
Click Tools and click Customize.
Click the Commands tab.
Click the drop-down arrow on the Save in combo box and click the map document in which the new command will be saved.
Scroll through the Categories list and click [UIControls].
Click New UIControl.
Click to select the UIToolControl as the UIControl Type.
Click Create and Edit.
Copy and paste the following code after all other code in the ThisDocument (Code) window.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
Close or minimize the VBA window.
In ArcMap, click Tools and click Customize.
Click the Commands Tab.
Click the drop-down arrow on the Save in combo box and click the map document in which the new control was saved.
Scroll through the Categories list and click [UIControls].
Click the Project.UIToolControl1 in the Commands list. Click and drag this button and drop it onto any toolbar.
Click Close.
Click the tool and click anywhere on the map.

No comments:

Post a Comment