Skip to content

Instantly share code, notes, and snippets.

@nesslopz
Forked from clauswitt/distance.js
Last active March 19, 2016 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nesslopz/5e8e33fc8d9a256adb39 to your computer and use it in GitHub Desktop.
Save nesslopz/5e8e33fc8d9a256adb39 to your computer and use it in GitHub Desktop.
Get the distance between two (world) coordinates - a nodejs module

distance.js

NodeJs module to calc distances between two points using coords. Originally cloned from https://gist.github.com/1604972. Response is in km

Installation

npm install distance.js --save

Usage

var distance = require('distance.js');

var start = {
  latitude: Coords,
  longitude: Coords
}
var end = {
  latitude: Coords,
  longitude: Coords
}

var distance = distance.getDistance(start, end) // Decimals is 2 by default.
var distance = distance.getDistance(start, end, 3) // Returns 3 decimals
console.log("Distance is: " + distance)
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* */
/* Simple node js module to get distance between two coordinates. */
/* */
/* Code transformed from Chris Veness example code - please refer to his website for licensing */
/* questions. */
/* */
/* */
/* Latitude/longitude spherical geodesy formulae & scripts (c) Chris Veness 2002-2011 */
/* - www.movable-type.co.uk/scripts/latlong.html */
/* */
/* */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/** Converts numeric degrees to radians */
if(typeof(Number.prototype.toRad) === "undefined") {
Number.prototype.toRad = function () {
return this * Math.PI / 180;
}
}
// start and end are objects with latitude and longitude
//decimals (default 2) is number of decimals in the output
//return is distance in kilometers.
exports.getDistance = function(start, end, decimals) {
decimals = decimals || 2;
var earthRadius = 6371; // km
lat1 = parseFloat(start.latitude);
lat2 = parseFloat(end.latitude);
lon1 = parseFloat(start.longitude);
lon2 = parseFloat(end.longitude);
var dLat = (lat2 - lat1).toRad();
var dLon = (lon2 - lon1).toRad();
var lat1 = lat1.toRad();
var lat2 = lat2.toRad();
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));
var d = earthRadius * c;
return Math.round(d * Math.pow(10, decimals)) / Math.pow(10, decimals);
};
0 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/nodejs', '/usr/bin/npm', 'publish' ]
2 info using npm@3.8.2
3 info using node@v4.4.0
4 verbose publish [ '.' ]
5 verbose stack Error: Failed to parse json
5 verbose stack Unexpected token 'd' at 13:16
5 verbose stack "keywords": [distance, coords, calc, latitude, longitude],
5 verbose stack ^
5 verbose stack at parseError (/usr/lib/node_modules/npm/node_modules/read-package-json/read-json.js:379:11)
5 verbose stack at parseJson (/usr/lib/node_modules/npm/node_modules/read-package-json/read-json.js:68:23)
5 verbose stack at /usr/lib/node_modules/npm/node_modules/read-package-json/read-json.js:48:5
5 verbose stack at /usr/lib/node_modules/npm/node_modules/graceful-fs/graceful-fs.js:78:16
5 verbose stack at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:380:3)
6 verbose cwd /home/nesslopz/Plantillas/Github/distance.js
7 error Linux 4.2.0-34-generic
8 error argv "/usr/bin/nodejs" "/usr/bin/npm" "publish"
9 error node v4.4.0
10 error npm v3.8.2
11 error file /home/nesslopz/Plantillas/Github/distance.js/package.json
12 error code EJSONPARSE
13 error Failed to parse json
13 error Unexpected token 'd' at 13:16
13 error "keywords": [distance, coords, calc, latitude, longitude],
13 error ^
14 error File: /home/nesslopz/Plantillas/Github/distance.js/package.json
15 error Failed to parse package.json data.
15 error package.json must be actual JSON, not just JavaScript.
15 error
15 error This is not a bug in npm.
15 error Tell the package author to fix their package.json file. JSON.parse
16 verbose exit [ 1, true ]
{
"name": "distance.js",
"version": "1.0.0",
"description": "A simple module to calc distance between 2 points using coords. Originally cloned from https://gist.github.com/1604972.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+ssh://git@github.com:nesslopz/distance.js.git"
},
"keywords": ["distance", "coords", "calc", "latitude", "longitude"],
"author": "Ness López <n@wingo.io>",
"license": "ISC",
"bugs": {
"url": "https://github.com/nesslopz/distance.js/issues"
},
"homepage": "https://github.com/nesslopz/distance.js"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment