Browse Source

Fix distance calculation to use 'point.lat' instead of 'lat'

pull/274/head
Diana Shkolnikov 9 years ago
parent
commit
537effa8bf
  1. 11
      middleware/distance.js
  2. 4
      test/unit/middleware/distance.js

11
middleware/distance.js

@ -1,4 +1,5 @@
var geolib = require('geolib'); var geolib = require('geolib');
var check = require('check-types');
function setup() { function setup() {
@ -13,14 +14,20 @@ function computeDistances(req, res, next) {
return next(); return next();
} }
if ( !(req.clean.hasOwnProperty('lat') && req.clean.hasOwnProperty('lon')) ) { if (!(check.number(req.clean['point.lat']) &&
check.number(req.clean['point.lon']))) {
return next(); return next();
} }
var point = {
latitude: req.clean['point.lat'],
longitude: req.clean['point.lon']
};
res.data.forEach(function (place) { res.data.forEach(function (place) {
// the result of getDistance is in meters, so convert to kilometers // the result of getDistance is in meters, so convert to kilometers
place.distance = geolib.getDistance( place.distance = geolib.getDistance(
{ latitude: req.clean.lat, longitude: req.clean.lon }, point,
{ latitude: place.center_point.lat, longitude: place.center_point.lon } { latitude: place.center_point.lat, longitude: place.center_point.lon }
) / 1000; ) / 1000;
}); });

4
test/unit/middleware/distance.js

@ -6,8 +6,8 @@ module.exports.tests.computeDistance = function(test, common) {
test('valid lat/lon and results', function(t) { test('valid lat/lon and results', function(t) {
var req = { var req = {
clean: { clean: {
lat: 45, 'point.lat': 45,
lon: -77 'point.lon': -77
} }
}; };
var res = { var res = {

Loading…
Cancel
Save