Skip to content

Instantly share code, notes, and snippets.

@ievans3024
Created September 12, 2023 18:57
Show Gist options
  • Save ievans3024/af673185ad3e26dc7d48dcf0aa30922c to your computer and use it in GitHub Desktop.
Save ievans3024/af673185ad3e26dc7d48dcf0aa30922c to your computer and use it in GitHub Desktop.
Typescript code for calculating distance between two gps coordinates.
/**
* Enumeration of Earth's radius for a given distance unit
* The value can be used as the radius value in a great circle calculation
* @readonly
* @enum {number}
*/
const enum EarthRadius {
FEET = 2.0925e+7,
KILOMETERS = 6378.0,
METERS = 6378000.0,
MILES = 3963.0,
}
/**
* Coordinate Point type
* @typedef {Object} CoordinatePoint
* @property {number} latitude latitude of the coordinate
* @property {number} longitude longitude of the coordinate
*/
type CoordinatePoint = {
latitude: number,
longitude: number,
};
/**
* Convert a Coordinate Point in degrees to a Coordinate Point in radians
* @param point {CoordinatePoint} the Coordinate Point with latitude and longitude expressed as degrees
* @returns {CoordinatePoint} coordinate point with latitude and longitude expressed as radians
*/
const convertToRadians = (point: CoordinatePoint): CoordinatePoint => {
// latitude and longitude are degree values
// there are 360 degrees in a circle and 2 * Math.PI radians in a circle
// 360 / (2 * Math.PI) = 57.29577951308232
// 1 radian = 57.29577951308232 degrees
// Divide degrees by this number to get the number of radians
const DEGREE_DIVIDER = 57.29577951308232;
return {
latitude: point.latitude / DEGREE_DIVIDER,
longitude: point.longitude / DEGREE_DIVIDER,
};
};
/**
* Measure the distance between two latitude/longitude coordinates. Uses great circle formula.
* @param from {CoordinatePoint} the first coordinate point
* @param to {CoordinatePoint} the second coordinate point
* @param precision {number} the decimal precision to use for the returned value, default is 2
* @param radius {EarthRadius} the radius by which to multiply the result, default is 3963.0 (returns miles)
* @returns {number} the distance between the two supplied coordinate points
*/
const getDistance = (
from: CoordinatePoint,
to: CoordinatePoint,
precision = 2,
radius: EarthRadius = EarthRadius.MILES,
): number => {
// convert coordinate point units from degrees to radians
const point1: CoordinatePoint = convertToRadians(from);
const point2: CoordinatePoint = convertToRadians(to);
// measure the distance between the two points in radians
const result: number = Math.acos(
(Math.sin(point1.latitude) * Math.sin(point2.latitude))
+ (Math.cos(point1.latitude) * Math.cos(point2.latitude) * Math.cos(point1.longitude - point2.longitude)),
);
// convert the distance in radians to the target unit of measurement
const distance = radius * result;
// round the distance to the desired precision
return parseFloat(distance.toFixed(precision));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment