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?

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)); 
|improve this answer|||||

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.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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