Skip to content

Instantly share code, notes, and snippets.

@theorm
Created July 24, 2015 14:19
Show Gist options
  • Select an option

  • Save theorm/fab42af68f6af48b2dfe to your computer and use it in GitHub Desktop.

Select an option

Save theorm/fab42af68f6af48b2dfe to your computer and use it in GitHub Desktop.
// Converts from degrees to radians.
Math.radians = function(degrees) {
return degrees * Math.PI / 180;
};
// Converts from radians to degrees.
Math.degrees = function(radians) {
return radians * 180 / Math.PI;
};
/**
* Returns the destination point from point having travelled the given distance on the
* given initial bearing (bearing normally varies around path followed).
*
* @param {Array} coord - coordinates (lat, lon) in degrees.
* @param {number} distance - Distance travelled, in same units as earth radius (default: metres).
* @param {number} bearing - Initial bearing in degrees from north.
* @returns {Array} Destination point. Coordinates (lat, lon) in degrees
*
* @example
* var p1 = [51.4778, -0.0015];
* var p2 = destinationPoint(p1, 7794, 300.7);
*
* From: http://www.movable-type.co.uk/scripts/latlong.html
*/
function destinationPoint(coord, distance, bearing) {
var radius = 6371e3;
var φ1 = Math.radians(coord[0]);
var λ1 = Math.radians(coord[1]);
// see http://williams.best.vwh.net/avform.htm#LL
var δ = distance / radius; // angular distance in radians
var θ = Math.radians(bearing);
var φ2 = Math.asin( Math.sin(φ1)*Math.cos(δ) +
Math.cos(φ1)*Math.sin(δ)*Math.cos(θ) );
var λ2 = λ1 + Math.atan2(Math.sin(θ)*Math.sin(δ)*Math.cos(φ1),
Math.cos(δ)-Math.sin(φ1)*Math.sin(φ2));
λ2 = (λ2+3*Math.PI) % (2*Math.PI) - Math.PI; // normalise to -180..+180°
return [Math.degrees(φ2), Math.degrees(λ2)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment