nativescript-geolocation
nativescript-geolocation copied to clipboard
Heading (direction) between 2 locations
I am trying to reproduce an app from my Suunto watch. Basically, it returns the heading between 2 locations. For example, "N" means that I am currently "north" of the original (first) location.
Suunto has a heading function (see below) that returns this info. Is it possible to have something similar with the geolocation plugin?
HEADING(LAT1, LON1, LAT2, LON2)
Returns the heading from current location to given location.
Script: retVal = Suunto.heading(lat, lon, lat2, lon2)
• lat1: type float, degrees, range [-90,90]
• lon1: type float, degrees, range [-180,180]
• lat2: type float, degrees, range [-90,90]
• lon2: type float, degrees, range [-180,180]
• retVal: type float, degrees, range [0,360]
Currently, I am calculation this using this code:
//Functions to calculate origin bearing
function radians(n) {
return n * (Math.PI / 180);
}
function degrees(n) {
return n * (180 / Math.PI);
}
function getBearing(startLat,startLong,endLat,endLong){
startLat = radians(startLat);
startLong = radians(startLong);
endLat = radians(endLat);
endLong = radians(endLong);
var dLong = endLong - startLong;
var dPhi = Math.log(Math.tan(endLat/2.0+Math.PI/4.0)/Math.tan(startLat/2.0+Math.PI/4.0));
if (Math.abs(dLong) > Math.PI){
if (dLong > 0.0)
dLong = -(2.0 * Math.PI - dLong);
else
dLong = (2.0 * Math.PI + dLong);
}
return (degrees(Math.atan2(dLong, dPhi)) + 360.0) % 360.0;
}