9

I have two GPS Coordinates

e.g. (Lat1, Long1) and (Lat2,Long2)

Could anybody please help me find the angle between those two points.

Values should be 0-360 degrees.

j0k
  • 22,600
  • 28
  • 79
  • 90
Chintan Patel
  • 329
  • 2
  • 3
  • 5

2 Answers2

9

Taken from this previous SO post:

float dy = lat2 - lat1;
float dx = cosf(M_PI/180*lat1)*(long2 - long1);
float angle = atan2f(dy, dx);
Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
  • 1
    What are `M_PI` and `atan2f`? – FaCoffee Dec 15 '16 at 14:28
  • 1
    @CF84: Please take a look at [this](http://stackoverflow.com/questions/15231466/whats-the-difference-between-pi-and-m-pi-in-objc) link for `M_PI`. For `atan2f` please take a look [here](http://stackoverflow.com/questions/7418353/angle-between-two-lines-is-wrong). – npinti Dec 16 '16 at 05:42
  • 1
    @FaCoffee `M_PI` is a **pi**. See: https://www.gnu.org/software/libc/manual/html_node/Mathematical-Constants.html `atan2f` is a `arc tangent of y/x`, see: https://en.cppreference.com/w/c/numeric/math/atan2 – Firzen Aug 16 '19 at 16:10
6

I suppose you mean the bearing to and not the angle between the locations: If (lat1,long1) is stored in a Location object loc1 and (lat2,long2) is stored in loc2 you get the bearing from loc1 to loc2 like this:

float bearing = loc1.bearingTo(loc2);

The result is in degrees east of true north and its the initial bearing (which is important if loc1 and loc2 are far apart from each other).

There are some other useful methods in the Location class, see here for more details: http://developer.android.com/reference/android/location/Location.html

EDIT: I assumed Android for the answer, but yes, the tags do not imply that ...

Stefan
  • 4,645
  • 1
  • 19
  • 35