我使用谷歌自动完成地址api,它返回所有所需的数据,但不邮政编码。知道如何在api响应中获得邮政编码吗?下面是这个api返回的my 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"
]
},
使用place_id
执行后续请求以获取位置详细信息。
- 对自动完成请求和详细信息请求使用相同的会话令牌
- 在详细信息请求 中使用fields参数
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"],
},
],
}