3

Error caption:

enter image description here

I'm using Google GMSAutocompleteViewController.

I have my API key in AppDelegate, the most recent pod for Swift 3 Xcode 8, the import GoogleMaps and import GooglePlacesAPI are at the top of the vc and in appDelegate, and my internet connection is fine.

When I was using Swift 2.2 whenever I typed in an address everything worked fine. Now that I upgraded to Swift 3 every now and then I'll make a mistake on an address, then the "Can't load search results" cloud caption appears. When I dismiss the vc and try again I keep getting the same caption. I tried shutting the app off and restarting the simulator but it won't let me access autocomplete services. I'm way under the 2500 free daily limit search because it's literally only me testing the app.

I also notice the Google AutoComplete docs says to use GMSPlacesClient.provideAPIKey("...") but the the GMSPlacesClient method provideAPIKey isn't available so I only use GMSServices.provideAPIKey(".."). I wonder is this part of the issue.

Does anyone know the fix for this issue?

AppDelegate:

import UIKit
import GoogleMaps
import GooglePlacesAPI
import SystemConfiguration

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    override init() {
        super.init()

        //Google Maps API Key
        GMSServices.provideAPIKey("WORKS")

        GMSPlacesClient.provideAPIKey("DOES NOT WORK")
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

my code from googlePlaces:

import UIKit
import GooglePlaces
import GoogleMaps

class ViewController: UIViewController {


  @IBAction func onLaunchClicked(sender: UIButton) {
    let acController = GMSAutocompleteViewController()
    acController.delegate = self
    present(acController, animated: true, completion: nil)
  }
}

extension ViewController: GMSAutocompleteViewControllerDelegate {

  func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    print("Place name: \(place.name)")
    print("Place address: \(place.formattedAddress)")
    print("Place attributions: \(place.attributions)")
    dismiss(animated: true, completion: nil)
  }

  func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
    print(".....\(error)")
    dismiss(animated: true, completion: nil)
  }

  func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    dismiss(animated: true, completion: nil)
  }
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256

1 Answers1

4

You should put GooglePlaces, GooglePlacePicker and GoogleMaps into your Podfile:

pod 'GooglePlaces'
pod 'GooglePlacePicker'
pod 'GoogleMaps'

And provide your API_KEY:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    GMSPlacesClient.provideAPIKey("API_KEY_HERE")

    return true
}

enter image description here

For more information, follow this guide: https://developers.google.com/places/ios-api/start

Danh Huynh
  • 2,337
  • 1
  • 15
  • 18
  • Dan thanks for your help. nah it's not the API key because I was having a problem with Firebase Password Reset and contacted them. They checked my API keys and said they are fine. Also the autocomplete works but I've had a couple of hiccups. If it were the API key it wouldn't;t work at all. It's something else I just don't know. Thanks again though :) – Lance Samaria Mar 28 '17 at 02:00
  • How did you get GMSPlacesClient to let you use the method: provideAPIKey()? I moved it inside didFinishLaunching and it says "Type 'GMSPlacesClient' has no member 'provideAPIKey'" – Lance Samaria Mar 28 '17 at 02:04
  • Did you import the GooglePlaces? **import GooglePlaces** – Danh Huynh Mar 28 '17 at 02:06
  • I just tried it before you responded and autocomplete only shows GooglePlacesAPI. I'll try reg GooglePlaces now – Lance Samaria Mar 28 '17 at 02:07
  • Nah didn't work: No such module 'GooglePlaces'. I just looked in my pods and I only have th import GooglePlacesAPI pod. I'm going to import just the import GooglePlaces pod now and try it. Maybe that's the reason??? I hope so – Lance Samaria Mar 28 '17 at 02:09
  • https://developers.google.com/places/ios-api/start I followed this guide and used CocoaPods to add frameworks. Did you open **xcworkspace** (NOT xcproject)? If yes, let rebuild it before write `import GooglePlaces` – Danh Huynh Mar 28 '17 at 02:15
  • @ Danh Huynh Yeah I always use xcworkspace with my pods. I'm using Firebase for my database so I have no choice.I installed CocoaPods for my frameworks too. I have a feeling it's because I used pod GooglePlacesAPI and not pod GooglePlaces. I just looked at the docs and it said to use GooglePlaces and 'GooglePlacePicker' which I didn't have. I just tried to install andI'm having some GooglMaps pod issues. when I straighten it out I'll contact you. I think you should change your answer to "try installing the GooglePlaces and 'GooglePlacePicker pods". If it works I'll accept it as the answer :) – Lance Samaria Mar 28 '17 at 02:21
  • yes, I think that's the problem. `pod 'GooglePlaces'` – Danh Huynh Mar 28 '17 at 02:24
  • I installed both GooglePlaces and 'GooglePlacePicker' pods, added GMSPlacesClient.provideAPIKey("myKey") to appDelegate and still nothing. No dice on this roll. Strange problem. I wonder do i have to remove the GooglePlacesAPI pod... – Lance Samaria Mar 28 '17 at 03:04
  • This is my demo project https://www.dropbox.com/s/q82upfzv0y930z6/Demo.zip?dl=0 You can try! – Danh Huynh Mar 28 '17 at 03:59
  • Thanks man! I'll look it over tomorrow and get back to you. I appreciate it :) – Lance Samaria Mar 28 '17 at 05:33
  • I tried yours and it works. We have the same pods, imports, and GMS methods. I have no idea why mines stopped working but your s does. I keep getting that dang cloud, smh. I'm super pisssed. Thanks for your help!!! – Lance Samaria Mar 28 '17 at 05:44
  • It's strange! Let replace my API_KEY with yours. Is it working? – Danh Huynh Mar 28 '17 at 05:56
  • @ Danh Huynh good idea, better yet great idea, I never thought of that... It's funny the most obvious thing to try didn't even cross my mind, smh. I open my app, copy the key, I'm about to paste and I said let me launch it 1 more time to see what happens and it works??? There is nothing I did over the past couple of hrs because I was sleeping. It's very odd. I'm going to keep your project and when it happens AGAIN I'm going to paste my key over yours. Pls update the answer to say I should install GooglePlaces & GooglePlacePicker pods so I can accept it. THANK YOU VERY MUCH! Good friggin idea! – Lance Samaria Mar 28 '17 at 08:34
  • yes, I updated! It took a lot of time to solve this problem. – Danh Huynh Mar 28 '17 at 08:39
  • @ Danh Huynh The reason why I said to add the GooglePlacePicker pod is because the link says it. You should include the link so other people know what to follow. Add Google Maps too. – Lance Samaria Mar 28 '17 at 08:40
  • @LanceSamaria yes, I did – Danh Huynh Mar 28 '17 at 08:42
  • @DanhHuynh After clicking on one of the searched result it is coming on my current place not on the place I clicked. Where I am going wrong? – Khushboo Dhote Jan 23 '18 at 12:39
  • I got my solution. I need to add mapView.camera = GMSCameraPosition(target: place.coordinate, zoom: 15, bearing: 0, viewingAngle: 0) in didAutocompleteWith method in extension. – Khushboo Dhote Jan 24 '18 at 05:05