Get Map Coordinates while in Layout View

Discussion created by LucieBoucher on Apr 12, 2012
Latest reply on Apr 17, 2012 by LucieBoucher

I have built a tool in C# that performs the following : when the user clicks on the map, the tool gets the coordinates, and then adds a new point feature to a feature class with several attributes being edited according to the location of the point.  Of course, the point is added exactly where the user clicked, on the map.

The tool works fine when I use it in DataView.  However, the users might use it also in Layout View (in fact they will mostly use it in Layout View).  When they do, a point is created, but does not appear at the proper location because the coordinates used are the paper coordinates in centimeters or inches instead of the map coordinates in meters or degrees.  I was therefore wondering how to get the dataframe's coordinates while working in the Layout View, with an OnMouseDown function.  A particular aspect of the problem is that on the Layout View, I display more than one dataframes, with the same coordinate system, but with different scales . 

Here is the code I use for the tool (everything is within a protected override void OnMouseDown function) :
//Get the coordinates
IPoint newSite = new Point();
IActiveView activeView = ArcMap.Document.ActiveView;
IScreenDisplay screenDisplay = activeView.ScreenDisplay;
IPoint clickedPoint = screenDisplay.DisplayTransformation.ToMapPoint(arg.X, arg.Y);
newSite.PutCoords(clickedPoint.X, clickedPoint.Y);
newSite.SpatialReference = clickedPoint.SpatialReference;

//Reproject if required
if (newSite.SpatialReference.Name != "GCS_North_American_1983")
    ISpatialReferenceFactory srFactory = new SpatialReferenceEnvironment();
    ISpatialReference sr;
    IGeographicCoordinateSystem gcs = srFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_NAD1983);
    sr = gcs;

//Skip a long and boring part where I determine strings to edit attributes...

//Get the Layer
ILayer layerPlants = FindLayer("Plants Lucie"); //Note : the FindLayer function loops through the datasets, and then through the layers, to return an ILayer
//Get the Feature Class
IFeatureLayer featurelayerPlants = layerPlants as IFeatureLayer;
IFeatureClass featureclassPlants = featurelayerPlants.FeatureClass;

//Make sure the FeatureClass has a proper geometry (point)
if (featureclassPlants.ShapeType == esriGeometryType.esriGeometryPoint)
    //Create the point
    IFeature plant = featureclassPlants.CreateFeature();
    plant.Shape = newSite;

    //Boring part where I edit the point attributes

    //Commit du nouveau point

    //Refresh the active view to see the new point

I suppose there could be an if/else condition such as "if we are working in dataview, get these coordinates using method A, else get the coordinates using method B", and that the key to those methods is probably linked to the screenDisplay.DisplayTransformation.ToMapPoint, but I don't know how to perform the switch, and how to extract the proper coordinates.

Thank you very much for your help, once again!