【问题标题】:autocomplete google address api自动完成谷歌地址api
【发布时间】:2021-10-19 11:18:24
【问题描述】:

我正在使用 google 自动完成地址 api,它返回所有必需的数据,但不返回邮政编码。知道如何在 api 响应中获取邮政编码。下面是这个 api 返回的我的 url 和 api 响应。

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=19becket st  &types=address&key=my_api_key

数据返回格式如下

{
            "description": "19 Becket St, Boston, MA, USA",
            "matched_substrings": [
                {
                    "length": 2,
                    "offset": 0
                },
                {
                    "length": 9,
                    "offset": 3
                }
            ],
            "place_id": "ChIJN9v2H4l744kRRg9DI6ctGhY",
            "reference": "ChIJN9v2H4l744kRRg9DI6ctGhY",
            "structured_formatting": {
                "main_text": "19 Becket St",
                "main_text_matched_substrings": [
                    {
                        "length": 2,
                        "offset": 0
                    },
                    {
                        "length": 9,
                        "offset": 3
                    }
                ],
                "secondary_text": "Boston, MA, USA"
            },
            "terms": [
                {
                    "offset": 0,
                    "value": "19"
                },
                {
                    "offset": 3,
                    "value": "Becket St"
                },
                {
                    "offset": 14,
                    "value": "Boston"
                },
                {
                    "offset": 22,
                    "value": "MA"
                },
                {
                    "offset": 26,
                    "value": "USA"
                }
            ],
            "types": [
                "premise",
                "geocode"
            ]
        },

【问题讨论】:

    标签: google-maps google-api googleplacesautocomplete


    【解决方案1】:

    使用place_idplace details 进行后续请求。

    https://maps.googleapis.com/maps/api/place/details/json
      ?fields=address_components
      &place_id=ChIJN9v2H4l744kRRg9DI6ctGhY
      &key=YOUR_API_KEY
    

    邮政编码将位于响应的address_components 字段中,类型为postal_code,类似于以下内容:

    {
    address_components":
            [
              { "long_name": "48", "short_name": "48", "types": ["street_number"] },
              {
                "long_name": "Pirrama Road",
                "short_name": "Pirrama Rd",
                "types": ["route"],
              },
              {
                "long_name": "Pyrmont",
                "short_name": "Pyrmont",
                "types": ["locality", "political"],
              },
              {
                "long_name": "Council of the City of Sydney",
                "short_name": "Sydney",
                "types": ["administrative_area_level_2", "political"],
              },
              {
                "long_name": "New South Wales",
                "short_name": "NSW",
                "types": ["administrative_area_level_1", "political"],
              },
              {
                "long_name": "Australia",
                "short_name": "AU",
                "types": ["country", "political"],
              },
              {
                "long_name": "2009",
                "short_name": "2009",
                "types": ["postal_code"],
              },
            ],
    }
    

    【讨论】:

    • 您好,谢谢,place_id 是什么? place_id 是国家或国家/地区独有的吗?同一个国家的不同州的place_id是否不同?
    • place_id 在自动完成请求中返回。检查您问题中的回答。
    猜你喜欢
    • 1970-01-01
    • 2017-04-25
    • 2023-03-22
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2017-09-16
    相关资源
    最近更新 更多