【发布时间】:2016-10-18 02:59:17
【问题描述】:
我真的不明白。我在 iPhone 上运行的 Meteor (v1.3.3, MongoDb v2.6.7) 应用程序中有以下代码,但它并不总是有效。有时会,有时不会:
Places = new Mongo.Collection("places");
if(Meteor.isServer) {
Places._ensureIndex({'loc':'2dsphere', name: 1});
}
if(Meteor.isClient) {
var maxDistance = 50;
...
latLng = Geolocation.latLng();
if (! latLng)
{
console.warn('Could not get location!');
return;
}
console.log('location: ' + latLng.lat + ' (' + typeof latLng.lat + '), ' + latLng.lng + ' (' + typeof latLng.lng + ')');
var placeNearby = Places.findOne({
loc: {
$near: {
$geometry: {
type: "Point",
coordinates: [latLng.lng, latLng.lat]
},
$maxDistance: maxDistance //meters
}
}
});
...
}
获取 placeNearby 后,我会执行 console.log('placeNearby', placeNearby)。有些时候它确实找到了一个地方,而有些时候它没有。
我通过 Xcode 在我的 iPhone 5s (iOS 9) 上运行它:
meteor run ios-device --mobile-server http://10.0.1.10:3000
当我测试这个时,我总是在同一个地方。我认为这可能是因为 GPS 坐标不精确,所以我将 maxDistance 增加到 50000,但它并没有改变任何东西。有趣的是,我的 console.log 没有为 placeNearby 输出 undefined。它似乎在输出一个空字符串。
但是,如果我在 MongoDb 终端中输入以下查询,它会检索一些结果:
db.places.find({
loc: {
$near: {
$geometry: {
type: "Point",
coordinates: [-73.56737205923496, 45.50178023952462]
},
$maxDistance: 50 //meters
}
}
})
【问题讨论】:
-
在 Mongo Shell 中,您正在查找而不获取结果。当你在 JS 中做 findOne 时有结果吗
-
是的,find 和 findOne 都在 Mongo shell 中返回结果。但我发现了罪魁祸首。看我的回答。
标签: ios mongodb cordova meteor geometry