AzimuthJS is a small, stand-alone script/module to calculate distance, bearing and direction between two points (given the latitude/longitude of those points).
South latitudes are negative, east longitudes are positive.
Link azimuth.min.js in your HTML :
Load exact version: Latest version is
<script src="https://cdn.jsdelivr.net/gh/theGreski/AzimuthJS@2.0.1/dist/azimuth.min.js"></script>Load a version range instead of an exact version:
<script src="https://cdn.jsdelivr.net/gh/theGreski/AzimuthJS@2.0/dist/azimuth.min.js"></script>Omit the version completely and use "latest" to load the latest one (not recommended for production usage):
<script src="https://cdn.jsdelivr.net/gh/theGreski/AzimuthJS@latest/dist/azimuth.min.js"></script>Load module in JS:
const azimuth = require('https://cdn.jsdelivr.net/gh/theGreski/AzimuthJS@latest/dist/azimuth.min.js');Load ESM module:
import azimuth from 'https://cdn.jsdelivr.net/gh/theGreski/AzimuthJS@latest/dist/azimuth.min.js';Node.js ESM:
import azimuth from 'azimuthjs';The azimuth function accepts coordinates of two points ({lat: latitude, lng: longitude}, {lat: latitude, lng: longitude}). For example London to New York:
azimuth({lat: 51.509865, lng: -0.118092}, {lat: 40.730610, lng: -73.935242})The output will look like this:
{
distance: 5564893,
units: "m",
bearing: 288,
formula: "great-circle",
direction: "W"
}Or wrap aroud try catch block to find any validation isues:
try {
azimuth({lat: 9999, lng: -200}, {lat: "abc", lng: null}, null)
} catch (e) {
console.error(e)
}You can configure the following options:
Here's an example specyfying all available options:
azimuth({lat: 51.509865, lng: -0.118092}, {lat: 40.730610, lng: -73.935242},
{
units: "mi",
formula: "great-circle",
distancePrecision: 3,
bearingPrecision: 3,
directionPrecision: 2
}
)The output will look like this:
{
formula: "great-circle",
distance: 3457.863,
units: "mi",
bearing: 288.308,
direction: "W"
}| Option | Type | Description | Default |
|---|---|---|---|
| units | string | A string indicating units of the distance. Supported units are: m, km, ft, yd, mi, nm |
m |
| formula | string | Calculation formula. Supported types are: great-circle, rhumb-line |
great-circle |
| distancePrecision | number | Rounding precision for distance. Between 0 and 15. |
0 |
| bearingPrecision | number | Rounding precision for bearing. Between 0 and 15. |
0 |
| directionPrecision | number | Precision for compass direction. Supported types are: 0 no directions, 1 Cardinal directions (N, E, S, W), 2 Intercardinal directions (N, NE, E, SE, S, SW, W, NW), 3 Secondary intercardinal directions (N, NNE, NE, ENE, E, ESE, SE, SSE, S, SSW, SW, WSW, W, WNW, NW, NNW) |
2 |
Released under the MIT License