This site has a basic algorithm:
// in javascript, not hard to translate... var y = Math.sin(dLon) * Math.cos(lat2); var x = Math.cos(lat1)*Math.sin(lat2) - Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon); var brng = Math.atan2(y, x).toDeg();
UPDATED: see full math to javascript matching algorithm
This will give you a number from 0 to 360, and this is just a simple search question:
var bearings = ["NE", "E", "SE", "S", "SW", "W", "NW", "N"]; var index = brng - 22.5; if (index < 0) index += 360; index = parseInt(index / 45); return(bearings[index]);
It is important to note that your load changes when you move on the ground. The algorithm above shows the original bearing, but if you travel long distances, your bearing will differ significantly when you reach your destination (if you travel a short distance [<several hundred kilometers], then this probably will not change enough to be a problem).
source share