我有以下文件,地址位于新西兰惠灵顿:
{
"name": "Wellington New Zealand",
"location": [
174.77623600000004,
-41.2864603
]
}
我使用谷歌地理编码来获取字符串"新西兰"的国家边界,如下所示:
geocoder.geocode('New Zealand', function(err, res) {
console.log(res);
});
结果如下:
{
"address_components": [{
"long_name": "New Zealand",
"short_name": "NZ",
"types": ["country", "political"]
}],
"formatted_address": "New Zealand",
"geometry": {
"bounds": {
"northeast": {
"lat": -29.2313419,
"lng": -175.831536
},
"southwest": {
"lat": -52.61941849999999,
"lng": 165.8694369
}
},
"location": {
"lat": -40.900557,
"lng": 174.885971
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": -34.1295578,
"lng": 179.0625357
},
"southwest": {
"lat": -52.61941849999999,
"lng": 165.8700053
}
}
},
"place_id": "ChIJh5Z3Fw4gLG0RM0dqdeIY1rE",
"types": ["country", "political"]
从上面的输出中可以看到,有geometry.bounds
和geometry.viewport
对象。我正在尝试查找基于bounds
的文档,如下所示:
db.collection.find({
"location": {
"$geoWithin": {
$box: [
[bounds.southwest.lng, bounds.southwest.lat],
[bounds.northeast.lng, bounds.northeast.lat]
]
}
}
});
不幸的是,这个查询没有返回任何文档,但当我使用viewport
时,我可以很好地获得文档:
db.collection.find({
"location": {
"$geoWithin": {
$box: [
[viewport.southwest.lng, viewport.southwest.lat],
[viewport.northeast.lng, viewport.northeast.lat]
]
}
}
});
你能解释一下为什么我不能用bounds
来获取文档,而可以用viewport
来获取文档吗?我应该如何正确处理?
更新:我已经研究过geometry.bounds
是边界,geometry.viewport
是视口
可以解释一下geometry.bounds
和geometry.viewport
之间的区别是什么,我应该使用什么?
30.335098600000038,59.9342802不在您(新西兰)的范围内。它在伊朗的一个荒无人烟的地方。