gpt4 book ai didi

node.js - MongoDB/ Mongoose :Getting incorrect distance with geoNear method using geoJSON

转载 作者:太空宇宙 更新时间:2023-11-04 00:38:03 25 4
gpt4 key购买 nike

我已将我的架构定义为

var locationSchema = new mongoose.Schema({
coords: {
type: [Number],
index: '2dsphere'
},
});

geoJSON 对象定义为

var geoOptions = {
spherical: true,
maxDistance: 5000,
distanceMultiplier : 0.001,
num: 10
}
loc.geoNear(point, geoOptions, function(error, results, stats){//do stuff});

其中 loc 是我的模型实例,我的观点是

var point = {
type: "Point",
coordinates: [lng,lat]
};

我的数据库中有两个带有坐标的文档

"coords" : [
18.9164127,
72.8245292
]

"coords" : [
18.935197,
72.8249342
]

我得到的两者之间的距离为 0.619 公里,而它应该超过 2 公里。无法弄清楚我在哪里做错了。任何帮助,将不胜感激。提前致谢

最佳答案

看起来没问题,您是否以[经度,纬度]的格式插入坐标?该顺序很重要,并且是 mongo 使用 geoNear 执行查询所使用的顺序。

关于node.js - MongoDB/ Mongoose :Getting incorrect distance with geoNear method using geoJSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38066349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com