2
double computeHeading(double latitude1, double longitude1, double latitude2, double longitude2)
{
    double degToRad = PI / 180.0;
    double phi1 = latitude1*degToRad;
    double phi2 = latitude2*degToRad;
    double lam1 = longitude1*degToRad;
    double lam2 = longitude2*degToRad;

    double x,y;
    x = cos(phi2) * sin(lam2-lam1);
    printf("X is %lf\n", x);
    y = cos(phi1) * sin(phi2) - sin(phi1) * cos(phi2) * cos(lam2-lam1);
    printf("Y is %lf\n", y);
    return atan2(x,y)*180/PI;
}

I am using the above function to determine the true bearing from North between two geographic coordinates.

I'm currently developing a small navigation widget which uses GPS data from Android sensors. The widget has an arrow facing towards a point away from the device's current location. The arrow's direction changes with the device's current location and azimuth to always face the distant point.

Here is a scenario:

I'm at a location, facing north, and another location has a bearing of 300 degrees(somewhat northwest of me). If I face towards south, without moving, my relative bearing to the distant location should be 120 degrees.

How can I find the relative bearing with accounting for the facing direction (azimuth)?

user3115201
  • 111
  • 2
  • 14
  • Take a look at http://stackoverflow.com/questions/25506470/how-to-get-to-an-angle-choosing-the-shortest-rotation-direction ... if that doesn't help I can explain further. – PeteB Nov 30 '15 at 00:44
  • problem is, how do i know my current angle based on where i'm facing? – user3115201 Nov 30 '15 at 01:06
  • i'm using a gps device, does the "course" provide the direction/angle where i'm heading? Considering i don't have that data, how do i know my current heading? – user3115201 Nov 30 '15 at 01:09
  • your original question was how to find the relative bearing given the facing direction. The link I posted shows how to calculate the minimum turn direction and angle between two headings (the angle returned is the relative bearing). You seem to be asking a different question in the comments. – PeteB Nov 30 '15 at 01:19
  • Your question in comments is "how do I know my current heading" in the context where you have only one location and no compass. The answer is you cannot. – PeteB Nov 30 '15 at 01:22
  • its because it is essential to know my current heading to find the relative bearing to another point. I've already solved how to calculate the minimum turn direction with your link, though i've used the gps course data(which only has value when you move because it draws a vector from your previous point, so you have to be constantly moving). A new problem arises is that it goes back to true bearing when i do not move, because of lack of course heading. – user3115201 Nov 30 '15 at 07:35

1 Answers1

2

There are a couple of ways you can work this out. The first, which is what you appear to be doing, assumes the earth is spherical. Relative bearings are calculated using Haversine formulation for great circle navigation. Given starting and ending points, this formulation finds the great circle passing through the two points. From this an initial bearing can be calculated. This great circle route is the shortest route between the two points, but suffers from the problem the bearing, in general, will not be constant along the route. Also, except under some very specific cases, the reverse bearing does not behave as you seem to expect and if you want to determine it in general, you will have to perform another calculation reversing the starting and ending points.

Another method you could use is the Rhumb line formulation. In this case, the bearing between the starting point and ending point is constant and would allow you to use the relation you have for the reverse course if you would like. Since this will in general differ from the great circle distance, following Rhumb lines will not result in the shortest path between the two points, but it does simplify the navigation by holding the course constant.

Both of these approaches are described in detail at Calculate distance, bearing and more between Latitude/Longitude points

Another formulation for great circle navigation which uses a more accurate representation of the earth's shape, an oblate spheriod, which is a special type of ellipsoid, is attributed to Vincenty with additional enhancements provided by Karney. In these cases, the formulation is quite a bit more complicated and is probably overkill for most applications, and performance is quite a bit worse than the Haversine formulations above. But these formulations provide much better accuracy if you need it.

Update:

Based on the comment below, the main issue is one of figuring out how far to turn. This will simply be the angle between the normals of the plane containing the great circles for the current heading and the desired heading. To get the normal for the plane on the current heading, you need your current location L and a point some distance away on the current heading, C. The normal is just V = L×C. To compute the normal for the plane containing the great circle along the desired heading, you only need to know a point along the desired route, which you already have in the form of your destination point, which we call D. You can then find the normal by U = L×D. The angle between them is given by θ = acos((U∙V)/(|U||V|)).

In order to find L, C and D you must convert the Latitude, Longitude, Altitude (LLA) coordinates into Earth Centered, Earth Fixed (ECEF) coordinates.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
andand
  • 17,134
  • 11
  • 53
  • 79
  • apparently the first solution is enough to give me the initial bearing i need. I don't have to care for the final bearing since the distances between latitude and longitude points is <500 meters since its a car application. ofcourse the bearing im getting from all these formulas is based from north. But the problem is that, what if my car doesn't face north? I need the angle to Point B(If my car is point A) to adjust to whatever im facing. Much like missile warning systems in games where an arrow pops out to warn you from the direction where it came from, while you're also constantly moving – user3115201 Nov 30 '15 at 07:48
  • 1
    @user3115201 okay... I think I understand better. I think I have an idea for this and will put some additional information into this later today to answer your question if somebody doesn't beat me to it. – andand Nov 30 '15 at 17:06
  • @user3115201 See the update and let me know if that answers the question for you. – andand Dec 02 '15 at 16:22