4

hello I am using google autocomplete Places in my IOS app. But the problem is its not working. and I am unable to get a current location of the user.

The search function is successfully showing places in autocomplete box but the only problem I am having is unable to get user's current location.This is the code for getting user current location I am using

class ViewController: UIViewController,CLLocationManagerDelegate {


    var locationManager = CLLocationManager()
    var placesClient : GMSPlacesClient?
    override func viewDidLoad() {
        super.viewDidLoad()

         locationManager.requestAlwaysAuthorization()

       run()

    }

 func run(){

        self.placesClient?.currentPlaceWithCallback({ (list: GMSPlaceLikelihoodList?, error: NSError?) -> Void in
            if let error = error {
                print("error: \(error.description)")
                return
            }

            for likelihood in list!.likelihoods {
                if let likelihood = likelihood as? GMSPlaceLikelihood {
                    let place = likelihood.place
                    print("Current Place name \(place.name) at likelihood \(likelihood.likelihood)")
                    print("Current Place address \(place.formattedAddress)")
                    print("Current Place attributions \(place.attributions)")

                    print("Current PlaceID \(place.placeID)")
                }
            }
        })
    }

I have added keys in info.plist as well and I am compiling app on my phone.

enter image description here

hellosheikh
  • 2,929
  • 8
  • 49
  • 115

1 Answers1

0

Add the following in viewDidLoad()

placesClient = GMSPlacesClient.shared()

And you put run() right after request authorization, the API might not be working the fist time you run the app since it's async and the API would respond with error because authorization hasn't been got at that time.

Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
zuchie
  • 48
  • 6