【问题标题】:Google Maps API region parameter for postcode search用于邮政编码搜索的 Google Maps API 区域参数
【发布时间】:2016-07-15 13:26:28
【问题描述】:

我想使用 Google Maps API 来获取邮政编码的坐标。

我正在使用此文档:https://developers.google.com/maps/documentation/geocoding/intro?csw=1

当我现在想搜索邮政编码 1200(瑞士日内瓦)时,我使用以下 URL:
https://maps.google.com/maps/api/geocode/json?region=ch&components=postal_code:1200

我得到了很多不同的结果,但在瑞士没有(结果在问题的末尾)。看起来 region 参数根本不起作用,因为我在其他国家得到了结果。

当我搜索 1201(也是 Genève)时,它可以工作。

知道为什么吗?我宁愿没有得到任何结果,也不愿得到错误的结果(其他国家的结果)

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1200",
               "short_name" : "1200",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Region Brüssel-Hauptstadt",
               "short_name" : "Region Brüssel-Hauptstadt",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Belgien",
               "short_name" : "BE",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "1200, Belgien",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 50.86307189999999,
                  "lng" : 4.4629024
               },
               "southwest" : {
                  "lat" : 50.8337598,
                  "lng" : 4.3999935
               }
            },
            "location" : {
               "lat" : 50.850308,
               "lng" : 4.4278228
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 50.86307189999999,
                  "lng" : 4.4629024
               },
               "southwest" : {
                  "lat" : 50.8337598,
                  "lng" : 4.3999935
               }
            }
         },
         "place_id" : "ChIJNa8iqqHcw0cRG4NyYbv66KY",
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "1200",
               "short_name" : "1200",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Wien",
               "short_name" : "Wien",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Wien",
               "short_name" : "Wien",
               "types" : [ "postal_town" ]
            },
            {
               "long_name" : "Wien",
               "short_name" : "Wien",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Österreich",
               "short_name" : "AT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "1200 Wien, Österreich",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 48.2646423,
                  "lng" : 16.4007913
               },
               "southwest" : {
                  "lat" : 48.2252299,
                  "lng" : 16.3618448
               }
            },
            "location" : {
               "lat" : 48.235668,
               "lng" : 16.373098
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 48.2646423,
                  "lng" : 16.4007913
               },
               "southwest" : {
                  "lat" : 48.2252299,
                  "lng" : 16.3618448
               }
            }
         },
         "place_id" : "ChIJs0PDoUQGbUcRI9VvmpZbVzQ",
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "1200",
               "short_name" : "1200",
               "types" : [ "postal_code", "postal_code_prefix" ]
            },
            {
               "long_name" : "Lissabon",
               "short_name" : "Lissabon",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Lissabon",
               "short_name" : "Lissabon",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Portugal",
               "short_name" : "PT",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "1200 Lissabon, Portugal",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 38.7177413,
                  "lng" : -9.1387655
               },
               "southwest" : {
                  "lat" : 38.7006072,
                  "lng" : -9.168078299999999
               }
            },
            "location" : {
               "lat" : 38.7096083,
               "lng" : -9.1539178
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 38.7177413,
                  "lng" : -9.1387655
               },
               "southwest" : {
                  "lat" : 38.7006072,
                  "lng" : -9.168078299999999
               }
            }
         },
         "place_id" : "ChIJT3JAUYI0GQ0R4Nc7DsHrABw",
         "types" : [ "postal_code", "postal_code_prefix" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "1200",
               "short_name" : "1200",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Mbombela",
               "short_name" : "Mbombela",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Mbombela",
               "short_name" : "Mbombela",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Ehlanzeni",
               "short_name" : "Ehlanzeni",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Mpumalanga",
               "short_name" : "MP",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Südafrika",
               "short_name" : "ZA",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Mbombela, 1200, Südafrika",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : -25.434559,
                  "lng" : 31.035606
               },
               "southwest" : {
                  "lat" : -25.5273621,
                  "lng" : 30.9615131
               }
            },
            "location" : {
               "lat" : -25.4922163,
               "lng" : 31.0048384
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : -25.434559,
                  "lng" : 31.035606
               },
               "southwest" : {
                  "lat" : -25.5273621,
                  "lng" : 30.9615131
               }
            }
         },
         "place_id" : "ChIJC-Upoo1J6B4RbhfORqGdY2s",
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "1200",
               "short_name" : "1200",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "København K",
               "short_name" : "København K",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "København",
               "short_name" : "København",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Dänemark",
               "short_name" : "DK",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "1200 København K, Dänemark",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 55.6788537,
                  "lng" : 12.581048
               },
               "southwest" : {
                  "lat" : 55.6774083,
                  "lng" : 12.5790574
               }
            },
            "location" : {
               "lat" : 55.6780957,
               "lng" : 12.5799054
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 55.6794799802915,
                  "lng" : 12.5814016802915
               },
               "southwest" : {
                  "lat" : 55.6767820197085,
                  "lng" : 12.5787037197085
               }
            }
         },
         "place_id" : "ChIJuYvG1hZTUkYREL3-tlYomQk",
         "types" : [ "postal_code" ]
      }
   ],
   "status" : "OK"
}

【问题讨论】:

  • 根据那些文档,区域代码“只会影响,而不是完全限制地理编码器的结果”

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


【解决方案1】:

就像 duncan 所说,region code 只会影响,而不是完全限制来自地理编码器的结果。要了解更多信息,请查看region biasing

如果您想要某个国家/地区的特定结果,请使用 component=country 而不是区域。

我尝试在瑞士国家/地区申请 postal_code:1200。我得到零结果。因此,请确保您使用的请求有效。

https://maps.google.com/maps/api/geocode/json?components=postal_code:1200|country:CH

如果我尝试了 postal_code:1201,我会得到您想要的具体结果。

https://maps.google.com/maps/api/geocode/json?components=postal_code:1201|country:CH

如果您确定 postal_code:1200 是瑞士的有效邮政编码,那么该 postal_code 可能不会被 Google 地图视为瑞士日内瓦的邮政编码。

也尝试检查我回答的相关SO question

【讨论】:

  • 在组件中使用国家很好,这正是我所需要的。 postal_code:1200 有点奇怪,因为谷歌似乎不知道它
猜你喜欢
  • 2017-02-15
  • 1970-01-01
  • 2012-10-23
  • 2013-04-25
  • 1970-01-01
  • 1970-01-01
  • 2017-01-16
  • 2018-01-03
  • 2011-11-28
相关资源
最近更新 更多