ios – 获取用户当前位置google api无效

前端之家收集整理的这篇文章主要介绍了ios – 获取用户当前位置google api无效前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
你好我在我的 IOS应用程序中使用谷歌自动完成位置.但问题是它不起作用.而且我无法获得用户的当前位置.

搜索功能成功显示自动填充框中的位置,但我遇到的唯一问题是无法获取用户的当前位置.这是获取用户当前位置的代码我正在使用

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)")
                }
            }
        })
    }

我也在info.plist中添加了密钥,我正在手机上编译应用程序.

enter image description here

解决方法

在viewDidLoad()中添加以下内容

placesClient = GMSPlacesClient.shared()

并且您在请求授权后立即放入run(),因为它是异步的,API可能无法在您运行应用程序时第一次运行,并且API会因为当时尚未获得授权而响应错误.

猜你在找的iOS相关文章