I tried found geo points by radius, I found tutorial explain how to does it.
Snippet from tutorial:
First we need to create a schema. The docs give us some examples on how to store geospatial data. We are going to use the legacy format for our example. It’s recommended to store the longitude and latitude in an array. The docs warn use about the order of the values, longitude comes first.
var LocationSchema = new Schema({
name: String,
loc: {
type: [Number], // [<longitude>, <latitude>]
index: '2d' // create the geospatial index
}
});
First you can create a method in your controller that can look something like this:
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;
coords[1] = req.query.latitude;
// 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);
});
}
Reference to tutorial: How to use Geospatial Indexing in MongoDB with Express and Mongoose
After implemented source from tutorial to my project I didn't receive from database correct points by radius (points were not inside radius).
My question is how can I receive geo points by radius ( kilometers or meters don't matter)?
Thanks, Michael.