Google Places Autocomplete SDK code example .. is not complete

I’m implementing the autocomplete feature of Googles Places SDK for iOS, which allows the user to start typing a location into a Google map search, to then get back a list of locations that they were probably about to type. There are lots of code examples that they’ve given to help us devs along, which is nice. However the code example that I decided to follow was not very .. complete. And I ended up losing a fair amount of time working out why.

The examples Im referring to are here. Fair play to Google – there are loads of really quite thorough examples on this page. I feel a little guilty moaning about it. But moan I must because the thing missing from my example left me with a really tricky problem to fix.

Heres the snippet :

func placeAutocomplete() {
  let filter = GMSAutocompleteFilter()
  filter.type = .establishment
  placesClient.autocompleteQuery("Sydney Oper", bounds: nil, filter: filter, callback: {(results, error) -> Void in
    if let error = error {
      print("Autocomplete error \(error)")
    if let results = results {
      for result in results {
        print("Result \(result.attributedFullText) with placeID \(result.placeID)")

Fairly self explantory- you send off some text, and you get back a list of things that you might be about to type, the obvious one in this example being Sydney Opera House. They don’t show you how to initialise that placesClient property though here, nor do they show you how to do that anywhere on that page, or mention it. So I did this obvious thing, and just created an instance of GMSPlacesClient, like this :

var placesClient = GMSPlaceClient()

This works, in that the code compiles. Then what happens is when you start trying to use this autocomplete query in code, you get back an error messgae from Google : “The request is missing a valid API key” (in amongst various other blurb. Curses, you think – theres something wrong with my setup on the Google Cloud setup page. Create another key, try that. Check how you initialised the google SDKs in your appDidFinishLaunching method. Double check all that. Check bundle identifiers. Create another key. Etc. I was in this circle of misery for several hours, and neither was I getting any help from the usual ports of call, StackOverflow etc.

In the end I delved into the Google Places SDK code itself for answers. And thats where I found it, in a comment about the query mentioned above : The places client must be called by the shared manager.

And there you have it – you cannot simply instantiate an instance of GMSPlacesClient. You must modify the above call so it looks like this :

GMSPlacesClient.shared().autocompleteQuery("Sydney Oper", bounds: nil, filter: filter, callback: {(results, error) -> Void in ..

And there you have it : with that subtle change to use the singleton rather than your own instance, the call works and the error disappears. I was unable to find this solution anywhere online, which is why Im writing it up here, in the hope that it saves some other poor unsuspecting fool several hours of chasing about trying to mend API keys that are not actually broken.

Leave a Reply

Your e-mail address will not be published. Required fields are marked *