我有以下文件,其所在地位于新西兰惠灵顿:
{
"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
是bounds和geometry.viewport
是viewport
可以解释一下geometry.bounds
和geometry.viewport
之间的差异以及我应该使用什么?
最佳答案
30.335098600000038、59.9342802 不在您(新西兰)的管辖范围内。它位于 middle-of-nowhere-Iran .
关于javascript - 为什么无法根据Google Maps地理编码geometry.bounds找到位置,但可以通过MongoDB中的geometry.viewport找到位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29155398/