【问题标题】:Get user's current location google api not working获取用户的当前位置 google api 不起作用
【发布时间】:2016-12-15 17:27:07
【问题描述】:

你好,我在我的 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 中添加了密钥,我正在手机上编译应用程序。

【问题讨论】:

    标签: ios swift google-places-api google-location-services


    【解决方案1】:

    viewDidLoad()中添加以下内容

    placesClient = GMSPlacesClient.shared()
    

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

    【讨论】:

      猜你喜欢
      • 2015-03-23
      • 2014-01-24
      • 2019-08-14
      • 1970-01-01
      • 2011-07-20
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多