@jollie/haversine

1.0.9 • Public • Published

Version Licence Build Coverage Downloads

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 kilometer
mi for mile
nmi for nautical mile
ft for foot
in for inch

Return value

Distance in unit

Readme

Keywords

Package Sidebar

Install

npm i @jollie/haversine

Weekly Downloads

1

Version

1.0.9

License

MIT

Unpacked Size

5.5 kB

Total Files

4

Last publish

Collaborators

  • thejellyfish