从另一个获取地理坐标和距离
Obtain geographic coordinate from another one and a distance
我想从我的 mongoDb 中检索特定距离范围内的每个点。每个元素都以这种方式存储其位置:[latitude, longitude] 其中纬度和经度是浮点值。
例如:我希望我的数据库中的每个点距离 [48.862586, 2.352210]
最多 100 公里
计算我的参考点和所有其他参考点之间的距离,以了解它是否低于限制距离听起来不是个好主意...有没有办法问 google 地图 API 或另一个人(或我自己)来做到这一点?
我会将此标记为 mongodb 问题。
Mongodb 支持地理空间函数:http://blog.mongolab.com/2014/08/a-primer-on-geospatial-data-and-mongodb/
根据您使用的 mongo 版本,它可能很简单:
db.places.find( { loc :
{ $near : [ 100 , 100 ],
$maxDistance: 10 }
} )
新的 geoJSON 功能看起来真的很棒 - 我自己还没有时间使用它,但它看起来超级灵活。我喜欢能够在由 DB 层处理的任意多边形中找到元素。 :)
感谢@AllanNienhuis,他让我处于领先地位。我使用 Node.js + Mongoose 的解决方案:
在Event.js中:
var mongoose = require('mongoose');
var EventSchema = new mongoose.Schema({
name: String,
pos : [Number],
type: String
});
EventSchema.index({ pos : '2dsphere' });
module.exports = mongoose.model('Event', EventSchema);
在eventController.js
exports.near = function(req, res) {
var point = JSON.parse(req.body.point);
var max = parseInt(req.body.max);
console.log(point);
Event.geoNear(point.pos, { spherical : true, maxDistance : max }, function (err, results, stats) {
if (err) res.json(JsonResponse.get(Code.generic_error, {error: err}));
else res.json(JsonResponse.get(Code.valid, results ));
});
};
很有魅力!
我想从我的 mongoDb 中检索特定距离范围内的每个点。每个元素都以这种方式存储其位置:[latitude, longitude] 其中纬度和经度是浮点值。
例如:我希望我的数据库中的每个点距离 [48.862586, 2.352210]
最多 100 公里计算我的参考点和所有其他参考点之间的距离,以了解它是否低于限制距离听起来不是个好主意...有没有办法问 google 地图 API 或另一个人(或我自己)来做到这一点?
我会将此标记为 mongodb 问题。
Mongodb 支持地理空间函数:http://blog.mongolab.com/2014/08/a-primer-on-geospatial-data-and-mongodb/
根据您使用的 mongo 版本,它可能很简单:
db.places.find( { loc :
{ $near : [ 100 , 100 ],
$maxDistance: 10 }
} )
新的 geoJSON 功能看起来真的很棒 - 我自己还没有时间使用它,但它看起来超级灵活。我喜欢能够在由 DB 层处理的任意多边形中找到元素。 :)
感谢@AllanNienhuis,他让我处于领先地位。我使用 Node.js + Mongoose 的解决方案:
在Event.js中:
var mongoose = require('mongoose');
var EventSchema = new mongoose.Schema({
name: String,
pos : [Number],
type: String
});
EventSchema.index({ pos : '2dsphere' });
module.exports = mongoose.model('Event', EventSchema);
在eventController.js
exports.near = function(req, res) {
var point = JSON.parse(req.body.point);
var max = parseInt(req.body.max);
console.log(point);
Event.geoNear(point.pos, { spherical : true, maxDistance : max }, function (err, results, stats) {
if (err) res.json(JsonResponse.get(Code.generic_error, {error: err}));
else res.json(JsonResponse.get(Code.valid, results ));
});
};
很有魅力!