Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am moving an object from one point to another and would like to update the heading of the dynamic display properties. I am using this code:

m_dynamicSymbolProperties.set_Heading(esriDynamicSymbolType.esriDSymbolMarker, heading);

But I am having trouble getting my heading right. Does anyone know how to calculate a heading based on two IPoints which have standard esriSRGeoCS_WGS1984 spatial references?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted
float getHeadingFromTwoPoints(IPoint A, IPoint B)
{
            double lat1 = degreesToRadians(A.Y);
            double lon1 = degreesToRadians(A.X);
            double lat2 = degreesToRadians(B.Y);
            double lon2 = degreesToRadians(B.X);

            double dLon = lon2 - lon1;
            double tmp0 = Math.Sin(dLon) * Math.Cos(lat2);
            double tmp1 = Math.Cos(lat1) * Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(dLon);
            double tmp = Math.Atan2(tmp0, tmp1);
            double tc1 = tmp % (2 * Math.PI);
            double theta = (float)(tc1 * (180 / Math.PI));
            float heading = (float)theta;
            heading = heading + 90; 
            return heading; 
}

double degreesToRadians(double input)
{
    return (input * (Math.PI / 180)); 
}
share|improve this answer
add comment

The heading of a 2D vector is simply the unit vector fed into the Atan2 function.

        //calculate the 2D vector components dX, dY
        double dX = x2 - x1;
        double dY = y2 - y1;

        //determine the magnitude of the vector
        double length = Math.Sqrt(dX * dX + dY * dY);

        //normalize the vector to a unit vector
        double nX = dX / length;
        double nY = dY / length;

        //get the angle making use of Atan2, multiply by 180/pi to get degrees
        double angle = Math.Atan2(nX, nY) * (180 / Math.PI);

In any solution, you'll have to determine what 'quadrant' the result ends up in, and adjust the angle from there. Depending of course what system of angles you want the result in (quadrant bearings, or 0 degrees = grid north, or 0 degrees = the positive X axis, etc).

Googling 'determine quadrant' or something will get you somewhere with this, and you'll see better diagrams than I could draw here as my Ascii art sucks :-) Or the ArcObjects API might have a function to do it all for you.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.