forked from pelias/api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance.js
42 lines (33 loc) · 932 Bytes
/
distance.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const _ = require('lodash');
const geolib = require('geolib');
function setup(prefix) {
return function (req, res, next) {
const opts = {
prefix: prefix || 'point.'
};
return computeDistances(req, res, next, opts);
};
}
function computeDistances(req, res, next, opts) {
// do nothing if no result data set
if (!res || !res.data) {
return next();
}
if (!(_.isFinite(req.clean[opts.prefix + 'lat']) &&
_.isFinite(req.clean[opts.prefix + 'lon']))) {
return next();
}
var point = {
latitude: req.clean[opts.prefix + 'lat'],
longitude: req.clean[opts.prefix + 'lon']
};
res.data.forEach(function (place) {
// the result of getDistance is in meters, so convert to kilometers
place.distance = geolib.getDistance(
point,
{ latitude: place.center_point.lat, longitude: place.center_point.lon }
) / 1000;
});
next();
}
module.exports = setup;