startUpdatingLocation返回nil

时间:2016-02-17 05:35:54

标签: ios swift location core-location cllocationmanager

我正在尝试使用CLLocation。每次我尝试调用startUpdatingLocation()方法时,当前纬度和经度的值总是为零。我在locationManager.delegate = self函数中添加了locationManager.requestWhenInUseAuthorization()viewDidLoad()

func findCurrentLocation() -> String{
    // GET LOCATION OF USER
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    dispatch_async(dispatch_get_main_queue(), {
        self.locationManager.startUpdatingLocation()
    })

    var lat = String(self.locationManager.location!.coordinate.latitude)
    var long = String(self.locationManager.location!.coordinate.longitude)

    var ll = lat + "," + long
    return ll
}

1 个答案:

答案 0 :(得分:1)

您无法立即阅读该位置。您需要等到回调您的didUpdateLocations委托方法。

如果您定位的是iOS 9,可以拨打requestLocation进行一次性位置更新,但仍然可以didUpdateLocations