[GIS] Get coordinates from current view using arcobjects

arcobjectsbookmarkscoordinates

I'm wondering if there's a way to get the longitude/latitude coordinates from the center of a data frame? The problem I'm faced with is this: I want to set a bookmark and have ArcMap tell me the coordinates of the center of that bookmark. (Without using a mouse. This all needs to be automated.)
This is akin to hovering one's mouse over the center of a map and reciting the long./lat. numbers that are shown at the bottom of the ArcMap window.

I'm pretty "green" when it comes to VBA programming in ArcMap 10.0. At first I thought I could utilize the "Info" tool, but now I'm guessing that I need to get the use the IEnvelope interface to calculate the center of the map, then grab the coordinates from that. Any ideas?

Best Answer

I would recommend using the IActiveView.Extent property. You can cast an IMap to an IActiveView and vice versa. http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#//001200000199000000

The Extent property will return an IEnvelope that has an X and Y Max and Min that you can use to calculate the center if you would like.
http://resources.arcgis.com/en/help/arcobjects-net/componenthelp/index.html#//002m00000169000000

To get the IActiveView instance you can use the IApplication.Document property and cast the Document to an IMxDocument. You can then get an IActiveView using IMxDocument.ActiveView property. http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#//004900000054000000

Some code-

Dim mxDocument As ESRI.ArcGIS.ArcMapUI.IMxDocument = CType(application.Document, ESRI.ArcGIS.ArcMapUI.IMxDocument) ' Explicit Cast
Dim activeView As ESRI.ArcGIS.Carto.IActiveView = mxDocument.ActiveView
Dim currentExtent As IEnvelope = activeView.Extent
//TODO::  Get Max/Min X and Y from currentExtent and calculate the center point.

Here is a gis.stackexchange post that describes taking a map x,y and converting it to a lat, long. Extracting lat/long from screen when using British National Grid projection in ArcGIS Desktop?

In particular, follow these steps-

I'm not that familiar the the British National Grid, but it appears that a datum transformation is required when projecting to WGS84.

So, use ISpatialReferenceFactory.CreateGeoTransformation to create the appropriate datum transformation, and ISpatialReferenceFactory.CreateGeographicCoordinateSystem to create the target (WGS84) spatial reference.

Finally, cast the point into IGeometry5 and call ProjectEx, passing the target spatial reference and the geotransformation. IPoint.X and IPoint.Y should then represent longitude and latitude of the point where the user clicked.