13
I am trying to set up a small project where I have a list of locations and can filter them according to a x km amount. Ex: Filter all places within a 5km radius of my current location.
I have a Json
with some places in my city and when I try to make a 2km filter the Json
returns blank, but if I step 50km as parameter all data is returned. I am not able to identify where I am missing.
Follows excerpt from the code:
var mongoose = require('mongoose');
var Location = mongoose.model('Location');
// create an export function to encapsulate the controller's methods
module.exports = {
index: function(req, res, next) {
res.json(200, {
status: 'Location API is running.',
});
},
findLocation: function(req, res, next) {
var limit = req.query.limit || 10;
// get the max distance or set it to 8 kilometers
var maxDistance = req.query.distance || 8;
// we need to convert the distance to radians
// the raduis of Earth is approximately 6371 kilometers
maxDistance /= 6371;
// get coordinates [ <longitude> , <latitude> ]
var coords = [];
coords[0] = req.query.longitude || 0;
coords[1] = req.query.latitude || 0;
// find a location
Location.find({
loc: {
$near: coords,
$maxDistance: maxDistance
}
}).limit(limit).exec(function(err, locations) {
if (err) {
return res.json(500, err);
}
res.json(200, locations);
});
}
};
That’s the Json
that I’m using:
[
{
"name": "Igreja Matriz",
"loc": [
-49.974762,
-23.160631
]
},
{
"name": "Prefeitura Jacarezinho",
"loc": [
-49.973597,
-23.159745
]
},
{
"name": "Lotérica",
"loc": [
-49.980461,
-23.164231
]
}
]
I pass the parameters by QueryString
thus: http://localhost:3000/api/locations?longitude=-49.978440&latitude=-23.169557&distance=2
How I get the desired result?
That one
maxDistance /= 6371
is not a resquicio of formula Haversine? Because in mongodb you will not use radians– user1576978
Did you set the coordinates to index your Collection? If so, what type of index did you use? Make sure you use the getIndexes() method in your seat collection on the Mongo console
– LF Ziron
@Lfziron I don’t know if it’s the index I’m thinking of, but if it is 2d.
– DiegoAugusto
@user1576978 I’m dividing by 111.12 now, I got more precision, but I have a margin of error of about 100 meters
– DiegoAugusto
@Techies this error is acceptable depending on the scale on which you are working and the geographical coordinate system. If you want greater precision, you should work with the UTM projection system. That way the reported radius will actually be in meters.
– Marcello Benigno
@Marcellobenigno understood, so this margin of error is normal the way my project is?
– DiegoAugusto
@Techies is yes!
– Marcello Benigno
@Marcellobenigno if you want to elaborate a question to explain about I will mark it as correct.
– DiegoAugusto