'autocomplete google address api
I am using google autocomplete address api, it returns all the required data but not postal code. Any idea how to get postal code in api response. Below is the my url and api response returned by this api.
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=19becket st &types=address&key=my_api_key
data return is in following format
{
"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"
]
},
Solution 1:[1]
Use the place_id
to do a followup request for place details.
- Use the same session token for both the autocomplete requests and details requests
- Use the fields parameter in the details requests
https://maps.googleapis.com/maps/api/place/details/json
?fields=address_components
&place_id=ChIJN9v2H4l744kRRg9DI6ctGhY
&key=YOUR_API_KEY
The zip code will be in the address_components field of the response with type postal_code
similar to the following:
{
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"],
},
],
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | jpoehnelt |