【问题标题】:CLLocation found nil after using CLLocationManagerDelegate使用 CLLocationManagerDelegate 后发现 CLLocation 为零
【发布时间】:2019-12-29 05:53:07
【问题描述】:

我想得到longitudelatitude,我在控制器中添加CLLocationManagerDelegate

这是我的代码

let locationManager = CLLocationManager()


func getWeatherInformation(){
    var currentLocation: CLLocation!

    locationManager.requestWhenInUseAuthorization()
    if( CLLocationManager.authorizationStatus() == .authorizedWhenInUse ||
        CLLocationManager.authorizationStatus() ==  .authorizedAlways){
        currentLocation = locationManager.location
    }

    let longitude = "\(currentLocation.coordinate.longitude)"
    let latitude = "\(currentLocation.coordinate.latitude)"
}



override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.delegate = self
    getWeatherInformation()

}

}

我还在info.plist中添加了位置项,请看截图

screenshot

问题是longitudelatitude,在运行应用程序并崩溃后发现为零。 问题出在哪里?

非常感谢

【问题讨论】:

    标签: swift cllocationmanager


    【解决方案1】:

    请求后可能无法立即提供位置。 请实现委托功能

    optional func locationManager(_ manager: CLLocationManager,  didUpdateLocations locations: [CLLocation])
    
    

    获取位置。

    【讨论】:

      猜你喜欢
      • 2018-12-10
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多