-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 52f8650
Showing
6 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
.DS_Store | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
.DS_Store | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Haversine | ||
A simple haversine formula module for Node.js | ||
|
||
I created this small module for an application I created and figured I would package it up to share. | ||
|
||
## Installation | ||
`$ npm install haversine` | ||
|
||
## Usage | ||
### haversine(start, end, options) | ||
|
||
var haversine = require('haversine') | ||
|
||
start = { | ||
latitude: 10, | ||
longitude: 11, | ||
} | ||
end = { | ||
latitude: 11, | ||
longitude: 10 | ||
} | ||
|
||
console.log(haversine(start, stop)) | ||
console.log(haversine(start, stop, {unit: 'km'})) | ||
|
||
## Future | ||
I plan on adding more options soon including a threshold check. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// haversine | ||
// By Nick Justice (niix) | ||
// https://github.com/niix/haversine | ||
|
||
var haversine = (function() { | ||
|
||
// convert to radians | ||
var toRad = function(num) { | ||
return num * Math.PI / 180 | ||
} | ||
|
||
return function haversine(start, end, options) { | ||
var miles = 3960 | ||
var km = 6371 | ||
options = options || {} | ||
|
||
R = options.unit === 'km' ? km : miles | ||
|
||
var dLat = toRad(end.latitude - start.latitude) | ||
var dLon = toRad(end.longitude - start.longitude) | ||
var lat1 = toRad(start.latitude) | ||
var lat2 = toRad(end.latitude) | ||
|
||
var a = Math.sin(dLat/2) * Math.sin(dLat/2) + | ||
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2) | ||
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)) | ||
|
||
return R * c | ||
} | ||
|
||
})() | ||
|
||
module.exports = haversine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "haversine", | ||
"version": "0.0.1", | ||
"description": "A simple haversine module", | ||
"main": "haversine.js", | ||
"directories": { | ||
"test": "test" | ||
}, | ||
"scripts": { | ||
"test": "./test/test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/niix/haversine" | ||
}, | ||
"keywords": [ | ||
"haversine", | ||
"distance", | ||
"coordinates", | ||
"geolocation" | ||
], | ||
"author": "Nick Justice (niix)", | ||
"license": "MIT" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var haversine = require('../haversine'); | ||
|
||
start = { | ||
latitude: 50.03, | ||
longitude: 05 | ||
} | ||
|
||
end = { | ||
latitude: 58.38, | ||
longitude: 03 | ||
} | ||
|
||
// inserting values directly | ||
console.log(haversine({latitude: 12, longitude: 11}, {latitude: 10, longitude: 10})) | ||
|
||
// using objects | ||
console.log(haversine(start, end)) | ||
|
||
// using objects with unit conversion | ||
console.log(haversine(start, end, {unit: 'km'})) |