为什么Google Geocode API会针对“位置”和“位置+邮政编码”(相同的地方)返回不同的坐标?

时间:2019-04-07 18:19:20

标签: google-maps google-maps-api-3 geocoding

在大多数国家(如果不是所有国家/地区),每个地区(address_components-“ types”:[“ locality”,“ political”])至少具有一个邮政编码,较大的城市具有多个邮政编码(address_components-“ types”: [ “邮政编码” ])。 但是大多数地区只有一个邮政编码。 当仅对一个邮政编码使用“ Google Geocode API for Localities”时,位置(例如:“ richterswil”)和位置加上相应的邮政编码(例如:“ 8805 + richterswil”)将返回不同的坐标。这在我看来是意外的,并使某些任务更加复杂 (应该找到用户输入的地点的坐标-有些会包含邮政编码,有些则不会,这会导致本质上完全相同的事物产生不同的坐标)。是什么原因以及如何对其进行正确处理(在所有情况下都获得相同的坐标而无需在输入字段中输入邮政编码)?

-我发现,当在Geocode API调用中不包括邮政编码时,搜索到的地点的坐标正好与Google在地图上(标签)显示地点名称的位置相同。 -当包含邮政编码时,坐标似乎随机移动了500-1000米。

我已使用以下API调用进行实验(添加您自己的API密钥):

  1. 没有邮政编码:

https://maps.googleapis.com/maps/api/geocode/json?address=richterswil&components=country:CH&key=YOUR_API_KEY&language=de

  1. 包括邮政编码:

https://maps.googleapis.com/maps/api/geocode/json?address=8805+richterswil&components=country:CH&key=YOUR_API_KEY&language=de

  1. 回复(无邮政编码)
{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Richterswil",
               "short_name" : "Richterswil",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Horgen",
               "short_name" : "Horgen",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Zürich",
               "short_name" : "ZH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Schweiz",
               "short_name" : "CH",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Richterswil, Schweiz",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 47.2335482,
                  "lng" : 8.7409239
               },
               "southwest" : {
                  "lat" : 47.18228,
                  "lng" : 8.661769999999999
               }
            },
            "location" : {
               "lat" : 47.2053892,
               "lng" : 8.7070691
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 47.2335482,
                  "lng" : 8.7409239
               },
               "southwest" : {
                  "lat" : 47.18228,
                  "lng" : 8.661769999999999
               }
            }
         },
         "place_id" : "ChIJO0JjJ6CxmkcRLdEGdhgDTXg",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}
  1. 响应(包括邮政编码)
{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "8805",
               "short_name" : "8805",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Richterswil",
               "short_name" : "Richterswil",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Horgen",
               "short_name" : "Horgen",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Zürich",
               "short_name" : "ZH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Schweiz",
               "short_name" : "CH",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "8805 Richterswil, Schweiz",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 47.2201589,
                  "lng" : 8.713399599999999
               },
               "southwest" : {
                  "lat" : 47.1962357,
                  "lng" : 8.679173199999999
               }
            },
            "location" : {
               "lat" : 47.2097073,
               "lng" : 8.696259999999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 47.2201589,
                  "lng" : 8.713399599999999
               },
               "southwest" : {
                  "lat" : 47.1962357,
                  "lng" : 8.679173199999999
               }
            }
         },
         "place_id" : "ChIJ-Ub3o--xmkcRNqm0YtWPm1U",
         "types" : [ "postal_code" ]
      }
   ],
   "status" : "OK"
}

1 个答案:

答案 0 :(得分:4)

这是因为地区和邮政编码具有不同的地区,所以它们不在同一地方。您可以在Google Maps中进行检查。

No Postal Code

Postal Code Included

  

但是大多数地区只有一个邮政编码。

我不认为这对您的代码来说是一个很好的假设,因为很多国家/地区在一个地方有多个邮政编码。

这就是Geocoding API response中存在postcode_localities[]字段的原因。

根据您的要求:

  

没有邮政编码:   https://maps.googleapis.com/maps/api/geocode/json?address=richterswil&components=country:CH&key=YOUR_API_KEY&language=de

此坐标将坐标指向“ Richterswil,Schweiz”的近似中心,根据地图在此中心进行标记,查看其在标签上方的位置:

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#place_id%3DChIJO0JjJ6CxmkcRLdEGdhgDTXg

  

包含的邮政编码:   https://maps.googleapis.com/maps/api/geocode/json?address=8805+richterswil&components=country:CH&key=YOUR_API_KEY&language=de

为此,它指向邮政编码的中心,如上面的google maps链接所示