Home > Back-end >  Get zip from googleapi autocomplete request
Get zip from googleapi autocomplete request

Time:05-03

I am sending the following request to the google autocomplete api in attempts to get a street address with the zip code included.

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=917&component=country:us&key=[MyApiKey]&types=address&bounds=defaultBounds&stricktBounds=true&address_components=postal_code&fields=address_components

I can't seem to figure out how to ask for the zip code in the query parameters. When I pass a full address with the zip in the input, google returns the zip code, but I would like it to always return the zip code.

I can make another request to the google maps api and pass the place id that I get from the autocomplete endpt and get the full address with the following request.

https://maps.googleapis.com/maps/api/place/details/json?fields=formatted_address&place_id=Eic1MDEgQ29sbGVnZSBBdmVudWUsIEdyZWVuc2Jvcm8sIE5DLCBVU0EiURJPCjQKMgldGdoYQBlTiBEjv0Tw4tNAARoeCxDuwe6hARoUChIJVXM4RSQZU4gRLNRdpst7vxAMEPUDKhQKEgmVWLxpQBlTiBGKDjnnY2NAzg&key=[my_Key]

But I would like to do a search and get the zip code all in one request.

Edit: The following works in javascript with the google api javascript package, but I cannot use javascript in my project...

https://maps.googleapis.com/maps/api/place/js/AutocompletionService.GetPredictions?1s501&fields=formatted_address&key=[my_key]

Thanks for any help!!

CodePudding user response:

The google api place textsearch endpoint can be used instead with field type formatted_address to return the zip code.

https://maps.googleapis.com/maps/api/place/textsearch/json?query=971&fields=formatted_address&inputtype=textquery&key=[my_key]
  • Related