tags:

views:

57

answers:

2
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#

+2  A: 

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.

jsmith
A: 

Here is the code to zoom and center on a lat / long, the above poster was helpful but his solution did not work for me.

mapControl.MapScale = mapControl.MapScale / 2; //for zooming
ISpatialReferenceFactory srFactory = new SpatialReferenceEnvironmentClass(); //move up top later 
IGeographicCoordinateSystem gcs = srFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984); //World lat / long format
ISpatialReference sr1 = gcs;
IPoint point = new PointClass(); 
point.SpatialReference = gcs;
point.PutCoords(-92.96000, 44.9227);
point.Project(mapControl.SpatialReference);
mapControl.CenterAt(point);
iterationx
You only asked how to convert lat/lon to a pointclass. Your code is exactly like mine, except you scale and center at a point, instead of drawing the point on the map. My solution gave the correct information needed to answer your question.
jsmith
@jsmith I wonder if you have to set the SpatialReference of the point before you putCoords? I seem to think so, if that's not the case then I must have copy pasted your sol incorrectly.
iterationx
@iterationx Yes, you do. In my code I set the spatial reference of the geometryshape, because I was plotting the point. There was no need to set the sr of the point itself. You are not using a geometryshape, so you can just set the spatial reference of the point itself.
jsmith