为什么无法根据谷歌地图地理编码geometry.bounds找到位置,但可以通过MongoDB中的geometry.vi

Why does unable to find locations based on Google Maps geocoding geometry.bounds but possible by geometry.viewport in MongoDB?

本文关键字:geometry 位置 可以通过 vi 中的 MongoDB bounds 谷歌地图 为什么 编码      更新时间:2023-09-26

我有以下文件,地址位于新西兰惠灵顿:

 {
    "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.boundsgeometry.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.boundsgeometry.viewport之间的区别是什么,我应该使用什么?

30.335098600000038,59.9342802不在您(新西兰)的范围内。它在伊朗的一个荒无人烟的地方。