Get current google api user location not working

Hi, I'm using Google Autocomplete. Places in my iOS app. But the problem is that it does not work. and I can’t get the current location of the user.

The search function successfully shows places in the autocomplete field, but the only problem I am facing cannot get the user's current location. This is the code to get the user's current location. I use

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 added the keys to info.plist and I am compiling the application on my phone.

enter image description here

+4
source share
2 answers

, , , , , .

placesClient.currentPlaceWithCallback({ (placeLikelihoods, error) -> Void in
  guard error == nil else {
    print("Current Place error: \(error!.localizedDescription)")
    return
  }

  if let placeLikelihoods = placeLikelihoods {
    for likelihood in placeLikelihoods.likelihoods {
      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)")
    }
  }
})
0

viewDidLoad()

:
placesClient = GMSPlacesClient.shared()

run() , API , , , API , .

0

Source: https://habr.com/ru/post/1650688/


All Articles