-2

在大多数(如果不是所有)国家/地区,每个地区(address_components - “types”:[“locality”,“political”])至少有一个邮政编码,较大的城市有多个邮政编码(address_components - “types”:[“postal_code” " ])。但大多数地区只有一个邮政编码。当使用只有一个邮政编码的地区的 Google Geocode API 时,地区(例如:“richterswil”)和地区加上相应的邮政编码(例如:“8805+richterswil”)返回不同的坐标。这在我看来是出乎意料的,并使某些任务比应有的复杂得多(想想为用户输入的地方查找坐标 - 有些会包括邮政编码,有些则不会,这会导致本质上完全相同的事物出现不同的坐标)。这是什么原因以及如何正确处理它(在所有情况下获取相同的坐标,而无需在输入字段中请求邮政编码)?

-我发现,当在 Geocode API 调用中不包括邮政编码时,搜索地点的坐标与谷歌在地图(标签)上显示地区名称的位置完全相同。- 包含邮政编码时,坐标似乎随机移动了 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"
}
4

1 回答 1

0

这是因为 localities 和 postal_codes 有不同的地区,它们不是同一个地方。你可以在谷歌地图上查看。

没有邮政编码

包含邮政编码

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

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

这就是该postcode_localities[]字段存在于Geocoding API 响应中的原因。

至于您的要求:

没有邮政编码: 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

对于这个,它指向邮政编码的中心,如上面的谷歌地图链接所示

于 2019-04-10T03:50:16.983 回答