我正在开发一个应用程序,重用Scotch's Create a MEAN Stack Google Map App Tutorial by Ahmed Haque的方法。
我正在尝试实现一个应用程序,使用Google Maps API绘制包含在MongoDB实例中的GeoJson文件中的坐标的Points、LineStrings和Polygons。
我使用Mongoose构建我的数据模式并查询我的MongoDB数据库。
我想找到最接近某个点P0的最近点CP,给定P0的纬度和经度以及给定的最大半径distance用于寻找感兴趣的点。 在给定的图像上,例如如果我插入2000(公里),我的查询将找到距离P0最多2000公里的所有点。在此示例中,它可能会给我P1和P2。
当我的Mongoose Schema中只有Points时,我能够做到这一点。
我只有标记(Points)的此Schema:
// Pulls Mongoose dependency for creating schemas
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// Creates a User Schema.
var MarkerSchema = new Schema({
username: {type: String, required: true},
location: {type: [Number], required: true}, // [Long, Lat]
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now}
});
// Indexes this schema in 2dsphere format
MarkerSchema.index({location: '2dsphere'});
module.exports = mongoose.model('mean-markers', MarkerSchema);
这是我的仅标记的旧查询
:
var User = require('./model.js');
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 reqVerified = req.body.reqVerified;
// Opens a generic Mongoose Query
var query = User.find({});
// ...include filter by Max Distance (converting miles to meters)
if (distance) {
// Using MongoDB's geospatial querying features
query = query.where('location').near({
center: {
type: 'Point',
coordinates: [long, lat]
},
// Converting meters to miles
maxDistance: distance * 1609.34,
spherical: true
});
}
});
它表现得非常好,我能够获得接近的分数。
然后,我更改了我的“模式”,使其更具动态性,并支持Polyline和Polygon。
我能够使用以下“模式”插入和绘制新的点、线和面:
var mongoose = require('mongoose');
var GeoJSON = require('geojson');
var Schema = mongoose.Schema;
// Creates a Location Schema.
var LocationSchema = new Schema({
name: {type: String, required: true},
location: {
type: {type : String, required: true},
coordinates : [Schema.Types.Mixed]
},
created_at: {type: Date, default: Date.now},
updated_at: {type: Date, default: Date.now}
});
LocationSchema.index({location: '2dsphere'});
module.exports = mongoose.model('mean-locations', LocationSchema);
这是我的Mongoose查询
:
var GeoObjects = require('./model.js');
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;
if (distance) {
query = GeoObjects.find({'location.type':'Point'})
.where('location.coordinates').near({
center: {
type: 'Point',
coordinates: [lat, long]
},
// Converting meters to miles
maxDistance: distance * 1609.34,
spherical: true
});
}
// Execute Query and Return the Query Results
query.exec(function(err, users) {
if (err)
res.send(err);
console.log(users);
// If no errors, respond with a JSON of all users that meet the criteria
res.json(users);
});
});
console.log(users);
输出undefined。
在我的queryCtrl.js中记录查询结果会出现以下错误信息:
name: "MongoError", message: "error processing query: ns=MeanMapApp.mean-locatio…ed error: unable to find index for $geoNear query", waitedMS: 0, ok: 0, errmsg: "error processing query: ns=MeanMapApp.mean-locatio…ed error: unable to find index for $geoNear 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;
console.log(lat,long,distance);
var points = GeoObjects.find({'location.type':'Point'});
var loc = parseFloat(points.location.coordinates);
console.log(JSON.stringify(loc));
if (distance) {
var query = points.near(loc, {
center: {
type: 'Point',
coordinates: [parseFloat(lat), parseFloat(long)]
},
// Converting meters to miles
maxDistance: distance * 1609.34,
spherical: true
});
}
});
这是一个标记示例:
{
"name": "user01",
"location": {
"type":"Point",
"coordinates": [102.0, 0.0]
}
}
$near操作符如何与距离和最大距离一起使用:
来自Ahmed Haque的Scotch's Making MEAN Apps with Google Maps(第二部分)。
MongoDB搜索参数$near及其关联属性maxDistance和spherical用于指定我们要覆盖的范围。 我们将查询体的距离乘以1609.34,因为我们希望将用户输入(以英里为单位)转换为 MongoDB期望的单位(以米为单位)。
- 为什么会返回
undefined
? - 这个问题可能是由我的Schema引起的吗?
- 我该如何解决这个问题?
如果您需要一些澄清,请在下面发表评论。
提前致谢。