mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
696 B
33 lines
696 B
9 years ago
|
var geolib = require('geolib');
|
||
|
|
||
|
|
||
|
function setup() {
|
||
|
|
||
|
return computeDistances;
|
||
|
}
|
||
|
|
||
|
function computeDistances(req, res, next) {
|
||
|
|
||
|
// do nothing if no result data set
|
||
|
if (!req.results || !req.results.data) {
|
||
|
return next();
|
||
|
}
|
||
|
|
||
|
if ( !(req.clean.hasOwnProperty('lat') && req.clean.hasOwnProperty('lon')) ) {
|
||
|
return next();
|
||
|
}
|
||
|
|
||
|
req.results.data.forEach(function (place) {
|
||
|
// the result of getDistance is in meters, so convert to kilometers
|
||
|
place.distance = geolib.getDistance(
|
||
|
{ latitude: req.clean.lat, longitude: req.clean.lon },
|
||
|
{ latitude: place.center_point.lat, longitude: place.center_point.lon }
|
||
|
) / 1000;
|
||
|
});
|
||
|
|
||
|
next();
|
||
|
}
|
||
|
|
||
|
|
||
|
module.exports = setup;
|