Finding Closest Points To A Certain Point Given Its Coordinates And Maximum Distance - Query Result Undefined Using Mongoose With Mean Stack
I've an issue that I wasn't able to solve in some days, even looking at related Stack Overflow Q/A. I'm developing an application reusing Scotch's Create a MEAN Stack Google Map Ap
Solution 1:
I don't understand what's beneath all your code, but I know a thing: If you are using Google's Radar Search, you must take into consideration that
The maximum allowed radius is 50 000 meters.
Just take a look at their Documentation
Meaning that if you try with higher radius, results could be Zero
Solution 2:
I finally managed to solve this issue.
Essentially, the issue was caused by the schema, since 2dIndex
was referred to a wrong field (type and coordinates)
.
I solved using the following Schema:
var mongoose = require('mongoose');
varGeoJSON = require('geojson');
varSchema = mongoose.Schema;
var geoObjects = newSchema({
name : {type: String},
type: {
type: String,
enum: [
"Point",
"LineString",
"Polygon"
]
},
coordinates: [Number],
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now}
});
// Sets the created_at parameter equal to the current time
geoObjects.pre('save', function(next){
now = newDate();
this.updated_at = now;
if(!this.created_at) {
this.created_at = now
}
next();
});
geoObjects.index({coordinates: '2dsphere'});
module.exports = mongoose.model('geoObjects', geoObjects);
And the following Query:
app.post('/query/', function(req, res) {
// Grab all of the query parameters from the body.var lat = req.body.latitude;
var long = req.body.longitude;
var distance = req.body.distance;
var query = GeoObjects.find({'type':'Point'});
// ...include filter by Max Distance if (distance) {
// Using MongoDB's geospatial querying features.
query = query.where('coordinates').near({
center: {
type: 'Point',
coordinates: [lat, long]
},
// Converting meters to milesmaxDistance: distance * 1609.34,
spherical: true
});
}
// Execute Query and Return the Query Results
query.exec(function(err, geoObjects) {
if (err)
res.send(err);
// If no errors, respond with a JSON
res.json(geoObjects);
});
});
I hope it will help someone!
EDIT
The schema I put over causes a bit of problems with LineStrings
and Polygons
.
Here are correct schemas which allow using geoQueries
linestring-model.js:
var mongoose = require('mongoose');
varSchema = mongoose.Schema;
// Creates a LineString Schema.var linestrings = newSchema({
name: {type: String, required : true},
geo : {
type : {type: String,
default: "LineString"},
coordinates : Array
},
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now}
});
// Sets the created_at parameter equal to the current time
linestrings.pre('save', function(next){
now = newDate();
this.updated_at = now;
if(!this.created_at) {
this.created_at = now
}
next();
});
linestrings.index({geo : '2dsphere'});
module.exports = mongoose.model('linestrings', linestrings);
polygon-model.js
var mongoose = require('mongoose');
varSchema = mongoose.Schema;
// Creates a Polygon Schema.var polygons = newSchema({
name: {type: String, required : true},
geo : {
type : {type: String,
default: "Polygon"},
coordinates : Array
},
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now}
});
// Sets the created_at parameter equal to the current time
polygons.pre('save', function(next){
now = newDate();
this.updated_at = now;
if(!this.created_at) {
this.created_at = now
}
next();
});
polygons.index({geo : '2dsphere'});
module.exports = mongoose.model('polygons', polygons);
LineString Insert:
{"name":"myLinestring","geo":{"type":"LineString","coordinates":[[17.811,12.634],[12.039,18.962],[15.039,18.962],[29.039,18.962]]}}
Polygon Insert:
{
"name" : "Poly",
"geo" : {
"type" : "Polygon",
"coordinates" : [
[
[25.774, -80.190], [18.466, -66.118],
[32.321, -64.757], [25.774, -80.190]
]
]
}
}
Post a Comment for "Finding Closest Points To A Certain Point Given Its Coordinates And Maximum Distance - Query Result Undefined Using Mongoose With Mean Stack"