'Google Place API - autocomplete - how to get ZIP-code?

How can I force the autocomplete function from Google Place API to complete the textView with zip codes?

I've tried using this link:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=20&types=postal_code&language=de&sensor=false&key=myKey

to obtain zip codes starting with 20, but it doesn't work. I have following responce from the server:

{
   "predictions" : [],
   "status" : "INVALID_REQUEST"
}

How can I make it works>



Solution 1:[1]

The problem with your request is that postal_code is not a valid type to pass to the AutoComplete endpoint. If you look at place types for the AutoComplete endpoint you can see the four allowed types are geocode, establishment, (regions), and (cities). The closest you can get to what your looking for is setting the type to "types=(regions)".

This will return any result that has one or more of the following types :

  • locality
  • sublocality
  • postal_code
  • country
  • administrative_area1
  • administrative_area2

Solution 2:[2]

You need to add two headers to request if you want to make this HTTP request (now you aren't using api) from an android device: An SHA-1 code, with an inclusion with header "x-android-cert" Your package name, with an inclusion with header "x-android-package"

Note that first you need to add this two params to credentials into your google console

As answered at this post Restricting usage for an Android key for a Google API

Solution 3:[3]

use the following request with the google api place textsearch endpoint

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

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 Bobbake4
Solution 2
Solution 3 micahkimel