Skip to content

Instantly share code, notes, and snippets.

@jesty
Last active September 26, 2019 02:59
Show Gist options
  • Save jesty/7b2a8e31d56c13a6adff to your computer and use it in GitHub Desktop.
Save jesty/7b2a8e31d56c13a6adff to your computer and use it in GitHub Desktop.
Compute heading
/*
* A heading is defined in degrees from true north, where headings are measured clockwise from true north (0 degrees).
* The parameter for this function are two array that identifies a geo point with latitude at position 0 and longitude
* at position 1.
* This function is similar to https://developers.google.com/maps/documentation/javascript/geometry. I developed because
* I need it on Meteor server side.
* The result is a value between -180 and 180.
*/
var computeHeading = function (start, end){
var lat1 = start[0]*Math.PI/180;
var lat2 = end[0]*Math.PI/180;
var lng1 = start[1]*Math.PI/180;
var lng2 = end[1]*Math.PI/180;
return Math.atan2( Math.sin(lng2-lng1) * Math.cos(lat2), Math.cos(lat1)*Math.sin(lat2)-Math.sin(lat1)*Math.cos(lat2)*Math.cos(lng2-lng1))*180/Math.PI;
}
@roblav96
Copy link

roblav96 commented Oct 4, 2015

holly crap I love you!!!

@zhen1991
Copy link

Hi!
Please can you declare more cause teh code is not working when i run my program :)
thnx for helping:)

@zhen1991
Copy link

i think i fix my error :)
thnx:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment