为什么Google Geocode API会为Localities and Localities + ZIP(同一个地方)返回不同的坐标?

问题描述 投票:-2回答:1

在大多数情况下,如果不是所有国家/地区,每个地区(address_components - “类型”:[“地点”,“政治”])至少有一个邮政编码,较大的城市有多个邮政编码(address_components - “types”:[“postal_code “])。但大多数地方只有一个邮政编码。当使用只有一个邮政编码的Google Geocode API for Localities时,地点(例如:“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"
}
google-maps google-maps-api-3 geocoding
1个回答
2
投票

这是因为地方和邮政编码有不同的地域,它们不是同一个地方。您可以在Google地图中查看此内容。

No Postal Code

Postal Code Included

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

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

这就是为什么postcode_localities[]场存在于Geocoding API response

至于你的要求:

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

这个坐标位于“Richterswil,Schweiz”的APPROXIMATE中心,在那里根据地图标记,看看它是如何在标签之上:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.