【问题标题】:Google Geocoding API - return any valid address based of zip codeGoogle Geocoding API - 根据邮政编码返回任何有效地址
【发布时间】:2020-03-28 08:31:45
【问题描述】:

我需要返回存在于邮政编码周围的任何(至少一个)有效地址。 国家永远都是一样的。

我怎样才能做到这一点?

谢谢,

【问题讨论】:

    标签: api geocoding


    【解决方案1】:

    您可以通过 2 个请求来完成此操作,将第一个请求的输出用作第二个请求的输入。最后列出了一些注意事项。

    请求 1
    根据邮政编码获取数据:

    curl --location --request GET 'https://maps.googleapis.com/maps/api/geocode/json?address=90058&key={{YOUR_API_KEY}}'
    

    从您的回复中,您将想要获取results[0].geometry.location 对象。您将在下一个查询中使用 latlng 值。上面的示例提供了以下内容:

       // … more above
            "formatted_address" : "Los Angeles, CA 90058, USA",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 34.045639,
                      "lng" : -118.1685449
                   },
                   "southwest" : {
                      "lat" : 33.979672,
                      "lng" : -118.2435201
                   }
                },
                "location" : {
                   "lat" : 34.00637469999999,
                   "lng" : -118.2234229
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 34.045639,
                      "lng" : -118.1685449
                   },
                   "southwest" : {
                      "lat" : 33.979672,
                      "lng" : -118.2435201
                   }
                }
             },
             "place_id" : "ChIJDeH8s8TIwoARYQFWkBcCzFk",
    
       // … more below
    

    请求 2
    根据经纬度获取数据

    curl --location --request GET 'https://maps.googleapis.com/maps/api/geocode/json?latlng=34.00637469999999,-118.2234229&key={{YOUR_API_KEY}}'
    

    注意!查询参数已从address 更改为latlng

    这可能会返回许多结果,您必须决定哪种类型符合您的要求,但我建议循环遍历结果数组并查找包含 street_addresspremisetypes 数组的条目并将该条目用作您的目标结果集。

    对于上面的示例,results[1] 将具有 "types" : [ "premise" ] 其关联的formatted_address 和完整的父对象如下:

    • 请参阅底部关于定位特定类型的说明
    "formatted_address" : "2727 E Vernon Ave, Vernon, CA 90058, USA",
    
    
       // … more above
          {
             "address_components": [
                {
                   "long_name": "2727",
                   "short_name": "2727",
                   "types": [
                      "street_number"
                   ]
                },
                {
                   "long_name": "East Vernon Avenue",
                   "short_name": "E Vernon Ave",
                   "types": [
                      "route"
                   ]
                },
                {
                   "long_name": "Vernon",
                   "short_name": "Vernon",
                   "types": [
                      "locality",
                      "political"
                   ]
                },
                {
                   "long_name": "Los Angeles County",
                   "short_name": "Los Angeles County",
                   "types": [
                      "administrative_area_level_2",
                      "political"
                   ]
                },
                {
                   "long_name": "California",
                   "short_name": "CA",
                   "types": [
                      "administrative_area_level_1",
                      "political"
                   ]
                },
                {
                   "long_name": "United States",
                   "short_name": "US",
                   "types": [
                      "country",
                      "political"
                   ]
                },
                {
                   "long_name": "90058",
                   "short_name": "90058",
                   "types": [
                      "postal_code"
                   ]
                },
                {
                   "long_name": "1822",
                   "short_name": "1822",
                   "types": [
                      "postal_code_suffix"
                   ]
                }
             ],
             "formatted_address": "2727 E Vernon Ave, Vernon, CA 90058, USA",
             "geometry": {
                "bounds": {
                   "northeast": {
                      "lat": 34.0065676,
                      "lng": -118.2228652
                   },
                   "southwest": {
                      "lat": 34.0057081,
                      "lng": -118.2245065
                   }
                },
                "location": {
                   "lat": 34.0061691,
                   "lng": -118.2236056
                },
                "location_type": "ROOFTOP",
                "viewport": {
                   "northeast": {
                      "lat": 34.0074868302915,
                      "lng": -118.2223368697085
                   },
                   "southwest": {
                      "lat": 34.0047888697085,
                      "lng": -118.2250348302915
                   }
                }
             },
             "place_id": "ChIJCQv2_sPIwoARZn8W2bF9a9g",
             "types": [
                "premise"
             ]
          },
    
       // … more below
    

    注意事项

    • 我假设你说的“邮政编码”是指美国。我只针对美国进行了测试,如果您需要其他国家/地区的支持,请考虑在请求 1 中将国家/地区代码添加到您的地址查询参数中。
    • 如果使用以邮政编码开头的这种方法,如果邮政编码代表人口密度非常低的大地理区域,您可能会遇到带有routestreet_address 但没有premise 的结果。我在“加利福尼亚州弗农 (90058) - 人口 112”和“堪萨斯州弗里波特城 (67049) - 人口 5”上对此进行了测试。弗里波特城没有返回 premise 类型的结果。
    • 如果您想查询特定类型以便不必搜索大型结果集,可以将查询参数 &result_type=premise 应用于请求 2。但是,如果给定 lat/lng 不存在任何结果使用您指定的类型,您将收到 results.status = ZERO_RESULTS
    • 可以应用多个 result_type 值,用管道分隔 (|)
    • 这里有关于反向地理编码的完整文档:https://developers.google.com/maps/documentation/geocoding/intro#ReverseGeocoding

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-09
      • 2016-07-20
      • 2018-10-17
      • 2020-12-13
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 2013-12-02
      相关资源
      最近更新 更多