Locate coordinates
.\LocateCoordinates\LocateCoordinates.cs
// Copyright 2010 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See the use restrictions.
// 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.DefenseSolutions;

namespace LocateCoordinates
{
    public partial class LocateCoordinates : Form
    {
        public LocateCoordinates()
        {
            InitializeComponent();
            // Add the world continents layer to the map for reference
            MapControl1.AddLayerFromFile(GetSdkDataPath() + @"World\Continents.lyr");
  
        }

        private void MapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {
            // clears existing point
            MapControl1.Refresh();
        }

        private void MapControl1_OnMouseUp(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseUpEvent e)
        {
            ConvertCoords(e.mapX, e.mapY);
            DrawPoint(e.mapX, e.mapY);
        }

        private void DrawPoint(double mapX, double mapY)
        {
            try
            {
                ESRI.ArcGIS.Display.IMarkerSymbol pSymbol;
                ESRI.ArcGIS.Display.IRgbColor pRGB;
                ESRI.ArcGIS.Geometry.IPoint pPoint;

                // Create a point geometry for the markersymbol
                pPoint = new ESRI.ArcGIS.Geometry.Point();
                pPoint.X = (double) mapX;
                pPoint.Y = (double) mapY;

                // Create a color for the markersymbol
                pRGB = new ESRI.ArcGIS.Display.RgbColor();
                pRGB.Red = 0;
                pRGB.Blue = 0;
                pRGB.Green = 0;

                // Create the markersymbol
                pSymbol = new ESRI.ArcGIS.Display.SimpleMarkerSymbol();
                pSymbol.Color = pRGB;
                pSymbol.Size = 10;
                object objSymbol = (object)pSymbol;

                // Add the markersymbol to the map at the clicked location
                MapControl1.DrawShape((ESRI.ArcGIS.Geometry.IGeometry) pPoint,ref objSymbol);
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message,"ERROR");
            }
            return;
        }


        private void ConvertCoords(double mapX, double mapY)
        {
            try
            {
                ESRI.ArcGIS.DefenseSolutions.ICoordinateTool pCoordTool;
                ESRI.ArcGIS.Geometry.IPoint vValue;
                int iFormat;
                object vFromDatum;
                object vToDatum;
                ESRI.ArcGIS.Geometry.IPoint pWGSPoint;
                ESRI.ArcGIS.Geometry.IPoint pOutPoint;
                string sDMS = "";
                string sUTM = "";
                string sMGRS = "";

                // Set the parameters to be used in the conversion.
                // The parameters are as follows:
                // vValue: The input coordinates
                // iFormat: The format of the input coordinates, where 1 = IPoint (decimal degrees)
                //                             2 = DMS
                //                             3 = UTM
                //                             4 = MGRS
                // vFromDatum: The datum of the input coordinates
                // vToDatum: The datum of the output coordinates
                // pWGSPoint: The output point with x/y coordinates in WGS1984 datum
                // pOutPoint: The output point with x/y coordinates in the output datum
                // sDMS: The output DMS coordinates
                // sUTM: The output UTM coordinates
                // sMGRS: The output MGRS coordinates

             
                vValue = new ESRI.ArcGIS.Geometry.Point();
                vValue.PutCoords(mapX, mapY);
                iFormat = 1;
                vFromDatum = 0; // => "WGS 1984 (WGS84)"
                vToDatum = 0;   // => "WGS 1984 (WGS84)"
                pWGSPoint = new ESRI.ArcGIS.Geometry.Point();
                pOutPoint = new ESRI.ArcGIS.Geometry.Point();

                pCoordTool = new ESRI.ArcGIS.DefenseSolutions.CoordinateToolClass();
                pCoordTool.ConvertLocation(vValue, iFormat, vFromDatum, vToDatum,
                                    ref pWGSPoint, ref pOutPoint,ref sDMS,ref sUTM,ref sMGRS);

                // Populate the coordinate text boxes with the coordinates calculated
                // by the ConvertLocation method.
                txtX.Text = pOutPoint.X.ToString();
                txtY.Text = pOutPoint.Y.ToString();
                txtDMS.Text = sDMS;
                txtUTM.Text = sUTM;
                txtMGRS.Text = sMGRS;
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.Message,"ERROR");
            }
            return;
        }

        private string GetSdkDataPath()
        {
            //get the ArcGIS path from the registry
            Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\ESRI\ArcGIS_SXS_SDK");
            string path = Convert.ToString(key.GetValue("InstallDir"));

            //set the of the logo
            string str = System.IO.Path.Combine(path, @"Samples\data\");

            if (!System.IO.Directory.Exists(str))
            {
                MessageBox.Show("Path :" + str + " does not exist!");
                return string.Empty;
            }

            return str;
        }

    }
}