Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
njj committed Jan 18, 2013
0 parents commit 52f8650
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.DS_Store
npm-debug.log
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.DS_Store
npm-debug.log
27 changes: 27 additions & 0 deletions README.md
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.
33 changes: 33 additions & 0 deletions haversine.js
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
24 changes: 24 additions & 0 deletions package.json
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"
}
20 changes: 20 additions & 0 deletions test/test.js
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'}))

0 comments on commit 52f8650

Please sign in to comment.