Locate coordinates in a MapControl
MapControlEvents.cpp
// 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.
// 


#include "MapControlEvents.h"

void MapControlEvents::OnAfterDraw(VARIANT display, long viewDrawPhase)
{}

void MapControlEvents::OnAfterScreenDraw(long hdc)
{}

void MapControlEvents::OnBeforeScreenDraw(long hdc)
{}

void MapControlEvents::OnDoubleClick(long button, long shift, long x,
                                     long y, double mapX, double mapY)
{}

void MapControlEvents::OnExtentUpdated(VARIANT displayTransformation,
                                       VARIANT_BOOL sizeChanged,
                                       VARIANT newEnvelope)
{}

void MapControlEvents::OnFullExtentUpdated(VARIANT displayTransformation,
                                           VARIANT newEnvelope)
{}

void MapControlEvents::OnKeyDown(long keyCode, long shift)
{}

void MapControlEvents::OnKeyUp(long keyCode, long shift)
{}

void MapControlEvents::OnMapReplaced(VARIANT newMap)
{}

void MapControlEvents::OnMouseDown(long button, long shift, long x, long y,
                                   double mapX,double mapY)
{}

void MapControlEvents::OnMouseMove(long button, long shift, long x, long y,
                                   double mapX, double mapY)
{}

void MapControlEvents::OnMouseUp(long button, long shift, long x, long y,
                                 double mapX, double mapY)
{
    ConvertCoords(mapX, mapY);
    DrawPoint(mapX, mapY);
}

void MapControlEvents::OnOleDrop(esriControlsDropAction dropAction,
                                 VARIANT dataObjectHelper, long* effect,
                                 long button, long shift, long x, long y)
{}

void MapControlEvents::OnSelectionChanged()
{}

void MapControlEvents::OnViewRefreshed(VARIANT ActiveView, long viewDrawPhase,
                                       VARIANT layerOrElement,
                                       VARIANT envelope)
{}

void MapControlEvents::DrawPoint(double mapX, double mapY)
{
    IMapPtr ipMap1;
    ipMap->get_Map(&ipMap1);
    IGraphicsLayerPtr ipGraphics;
    ipMap1->get_BasicGraphicsLayer(&ipGraphics);
    IGraphicsContainerPtr ipGContainer = ipGraphics;
    ipGContainer->DeleteAllElements();
    IPointPtr ipPoint(CLSID_Point);
    ipPoint->PutCoords(mapX, mapY);
    IRgbColorPtr ipRgb(CLSID_RgbColor);
    ipRgb->put_Red(0);
    ipRgb->put_Green(0);
    ipRgb->put_Blue(0);
    IMarkerSymbolPtr ipMarker(CLSID_SimpleMarkerSymbol);
    ipMarker->put_Color(ipRgb);
    ipMarker->put_Size(10);
    IElementPtr ipElement(CLSID_MarkerElement);
    ipElement->put_Geometry(ipPoint);
    IMarkerElementPtr(ipElement)->put_Symbol(ipMarker);
    ipGContainer->AddElement(ipElement, 0);
    ipMap->Refresh(esriViewBackground);
}

void MapControlEvents::ConvertCoords(double mapX, double mapY)
{

     HRESULT hr;

    //Create ICoordinateTool
    ICoordinateToolPtr ipCoordTool(CLSID_CoordinateTool);

    /*
     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
    */


    CComVariant vValue;
    CComVariant vFrom(0);  //"WGS 1984 (WGS84)"
    CComVariant vTo(0);    //"WGS 1984 (WGS84)"
    int iFormat = 1;
    CComBSTR bstrDMS, bstrUTM, bstrMGRS;

    //input point
    IPointPtr ipPoint(CLSID_Point);
    ipPoint->PutCoords(mapX, mapY);
    IPoint * pPoint = ipPoint;
    //vValue = (IPoint*)pPoint;
    vValue = pPoint;

    //output points
    IPointPtr ipWGS84Point(CLSID_Point);
    IPointPtr ipDatumPoint(CLSID_Point);

    hr = ipCoordTool->ConvertLocation(vValue, iFormat, vFrom, vTo, &ipWGS84Point, &ipDatumPoint, &bstrDMS, &bstrUTM, &bstrMGRS);
    if(SUCCEEDED(hr))
    {
        //get x/y values from the point
        double xx, yy;
        ipDatumPoint->QueryCoords(&xx, &yy);
        DisplayResult(xx, yy, bstrDMS, bstrUTM, bstrMGRS);
    }

}

void MapControlEvents::DisplayResult(double x, double y, CComBSTR dms, CComBSTR utm, CComBSTR mgrs)
{
    char xbuffer[20];
    char ybuffer[20];
    sprintf(xbuffer, "%f", x);
    sprintf(ybuffer, "%f", y);
    USES_CONVERSION;
    char* c1 = OLE2A(dms);
    char* c2 = OLE2A(utm);
    char* c3 = OLE2A(mgrs);

    foreach (QWidget *widget, QApplication::allWidgets())
    {
        QString name = widget->objectName();
        if(name == "XObj")
        {

            QLineEdit *lineEdit = static_cast<QLineEdit*> (widget);
            lineEdit->setText(xbuffer);
        }
        else if (name == "YObj")
        {
            QLineEdit *lineEdit = static_cast<QLineEdit*> (widget);
            lineEdit->setText(ybuffer);
        }
        else if (name == "DMSObj")
        {
            QLineEdit *lineEdit = static_cast<QLineEdit*> (widget);
            lineEdit->setText(c1);
        }
        else if (name == "UTMObj")
        {
            QLineEdit *lineEdit = static_cast<QLineEdit*> (widget);
            lineEdit->setText(c2);
        }
        else if (name == "MGRSObj")
        {
            QLineEdit *lineEdit = static_cast<QLineEdit*> (widget);
            lineEdit->setText(c3);
        }

    }
}