Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate angle between two Geographical/GPS coordinates?

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.

like image 800
Chintan Patel Avatar asked Mar 05 '12 11:03

Chintan Patel


2 Answers

Taken from this previous SO post:

float dy = lat2 - lat1;
float dx = cosf(M_PI/180*lat1)*(long2 - long1);
float angle = atan2f(dy, dx);
like image 180
npinti Avatar answered Sep 21 '22 19:09

npinti


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 ...

like image 37
Stefan Avatar answered Sep 22 '22 19:09

Stefan