gps-distance
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -7,3 +7,8 @@ var RADIUS = 6371; | ||
var Distance = function(fromLat, fromLon, toLat, toLon) { | ||
var getDistance = function(from, to) { | ||
var fromLat = from[0]; | ||
var fromLon = from[1]; | ||
var toLat = to[0]; | ||
var toLon = to[1]; | ||
var dLat = toRad(toLat - fromLat); | ||
@@ -21,2 +26,15 @@ var dLon = toRad(toLon - fromLon); | ||
module.exports = Distance; | ||
var measurePath = function(points) { | ||
return points.reduce(function(memo, point) { | ||
var distance = memo.lastPoint === null ? 0 : getDistance(memo.lastPoint, point); | ||
return { lastPoint: point, distance: distance + memo.distance }; | ||
}, { lastPoint: null, distance: 0 }).distance; | ||
}; | ||
module.exports = function(fromLat, fromLon, toLat, toLon) { | ||
if(typeof fromLat === 'number'){ | ||
return getDistance([fromLat, fromLon], [toLat, toLon]); | ||
} else { | ||
return measurePath(fromLat); | ||
} | ||
}; |
{ | ||
"name": "gps-distance", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Node module for computing distances between GPS coordinates", | ||
@@ -5,0 +5,0 @@ "private": false, |
@@ -15,6 +15,26 @@ gps-distance | ||
var result = distance(45.527517, -122.718766, 45.373373, -121.693604) | ||
// Between two points: | ||
var result = distance(45.527517, -122.718766, 45.373373, -121.693604); | ||
// result is 81.78450202539503 | ||
``` | ||
// Along a path: | ||
var path = [ | ||
[45.527517, -122.718766], | ||
[45.373373, -121.693604], | ||
[45.527517, -122.718766] | ||
]; | ||
var result2 = distance(path); | ||
// result2 is 163.56900405079006 | ||
``` | ||
Notes | ||
----- | ||
Distances are returned in kilometers and computed using the Haversine formula. |
@@ -12,3 +12,3 @@ 'use strict'; | ||
it('should measure distances correctly', function(done) { | ||
it('should measure a distance correctly', function(done) { | ||
@@ -21,4 +21,29 @@ var libResult = distance(45.527517, -122.718766, 45.373373, -121.693604) | ||
it('should measure the distance of a short path correctly', function(done) { | ||
var path = [ | ||
[45.527517, -122.718766], | ||
[45.373373, -121.693604] | ||
]; | ||
var libResult = distance(path); | ||
libResult.should.be.approximately(81.784, 0.1); | ||
done(); | ||
}); | ||
it('should measure the distance of a longer path correctly', function(done) { | ||
var path = [ | ||
[45.527517, -122.718766], | ||
[45.373373, -121.693604], | ||
[45.527517, -122.718766] | ||
]; | ||
var libResult = distance(path); | ||
libResult.should.be.approximately(81.784 * 2, 0.1); | ||
done(); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4963
62
39