Search code examples
c#arcobjects

Converting Lat / long to PointClass


IPoint pPoint = new ESRI.ArcGIS.Geometry.PointClass();
pPoint.PutCoords(-92.96000, 44.9227); //This should be near Minneapolis
mapControl.CenterAt(pPoint); //mapControl is a AxMapControl

When I run this code the point always ends up near Kansas. Can anyone help me convert lat / longs to an PointClass that will work properly?

I'm using VS2010 ArcEngine 10 C#


Solution

  • There is a lot more to this than you have currently given. Both a lat/long point and your map have a specific spatial reference. If they do not match, it is likely your point will plot in an unexpected way.

    The point you are showing is a standard Latitude/Longitude point. Which is likely Nad83 (North American), or WGS84 (World). These are Spatial References with a Geographical Coordinate System. You are likely trying to plot the point on a Projected Coordinate System.

    You need to make your MapControl's Spatial Reference match the types of points you are trying to plot.

    Since I do not know the Spatial Reference of your Map, I can only give you an example of translating a Lat/Lon into what the MapControl's current spatial reference is.

    ISpatialReferenceFactory srFactory = new SpatialReferenceEnvironmentClass();
    
    IGeographicCoordinateSystem gcs = srFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
    ISpatialReference sr1 = gcs;
    
    IPoint point = new PointClass() as IPoint;
    point.PutCoords(-92.96000, 44.9227);
    
    IGeometry geometryShape;
    geometryShape = point;
    geometryShape.SpatialReference = sr1;
    
    geometryShape.Project(mapControl.SpatialReference);
    
    mapControl.DrawShape(geometryShape);
    

    This takes your point and projects it to the MapControls current spatial reference, then plots the point.

    Good Luck.