@jollie/haversine
v1.0.9
Published
Haversine formula to determine distance between 2 points
Downloads
2
Readme
haversine
Haversine formula to determine distance between 2 points in few lines of code
Formula applied is taken from https://en.wikipedia.org/wiki/Haversine_formula
Install
yarn add @jollie/haversine
or
npm install @jollie/haversine
Usage
import haversine from '@jollie/haversine';
// ... coordinates example
const a = [4.8668945, 36.7699898];
const b = [3.9354349, 36.6988394];
let dist;
// Distance in meters from array
dist = haversine(a, b);
// Output 83381.53382511878
console.log(dist);
// Distance in meters from object
dist = haversine(
{ longitude: 4.8668945, latitude: 36.7699898 },
{ longitude: 3.9354349, latitude: 36.6988394 },
);
// Example from GeoJSON points
const pointA = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [4.8668945, 36.7699898],
},
};
const pointB = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [3.9354349, 36.6988394],
},
};
// Distance between geoJSON points
dist = haversine(pointA.geometry.coordinates, pointB.geometry.coordinates);
//-------
// Units support
//-------
// Distance in miles
dist = haversine(a, b, 'mi');
// Distance in nautical miles
dist = haversine(a, b, 'nmi');
// Distance in feet
dist = haversine(a, b, 'ft');
// Distance in inches
dist = haversine(a, b, 'in');
// Distance in Km
dist = haversine(a, b, 'km');
Params
haversine(a, b, unit='m');
| Prop | Type | Note |
|--------|---------------------|-------------------------------------------|
| a
| array
or object
| [lon, lat]
or { longitude, latitude }
|
| b
| array
or object
| [lon, lat]
or { longitude, latitude }
|
| unit
| string
| Unit param is optional (default is meter) |
Supported units
m
for meter (default)km
for kilometermi
for milenmi
for nautical mileft
for footin
for inch
Return value
Distance in unit