Skip to content

Production-ready Haversine distance module for accurate great-circle distance between latitude/longitude points on Earth.

License

NeaByteLab/Haversine-Distance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🌐 Harversine-Distance

This module provides a robust, production-ready Haversine distance function for calculating the shortest (great-circle) distance between two latitude/longitude points on the Earth’s surface.


🔍 How It Works

Uses the Haversine formula to calculate distance:

  • Accounts for Earth's curvature
  • More accurate than simple Pythagorean for global coordinates

🚀 Usage

1. Install

Add the file to your project as src/index.js or use in your own module structure.

2. Import

const calculateHaversine = require('./src/index')

const firstLocation = { lat: -6.2, lon: 106.8167 }
const secondLocation = { lat: -7.25, lon: 112.75 }

const distanceKm = calculateHaversine(firstLocation, secondLocation, 'km')
console.log('Distance (km):', distanceKm)

const distanceM = calculateHaversine(firstLocation, secondLocation, 'm')
console.log('Distance (m):', distanceM)

🧑‍💻 API

calculateHaversine(firstLocation, secondLocation, distanceUnit)

  • firstLocation { lat: number, lon: number }
  • secondLocation { lat: number, lon: number }
  • distanceUnit: "km" or "m"

Returns:

  • Number (distance between points, in requested unit)

Throws error if:

  • Parameters are missing or invalid
  • Distance unit is not "km" or "m"

✅ Test

Unit tests with Jest:

npm test

Test file: test/haversine.test.js


📜 License

MIT License © 2025 NeaByteLab

About

Production-ready Haversine distance module for accurate great-circle distance between latitude/longitude points on Earth.

Topics

Resources

License

Stars

Watchers

Forks