swift - 没有 swift 调用 didUpdateHeading

标签 swift ios9 cllocationmanager

我正在尝试获取设备的航向信息,以便使用磁航向和真实航向之间的差异来确定用户位置的磁偏角。为此,我有一个 Helper 类和我的 MainVc (mvc)。在我的助手类 init 方法中,我执行以下操作:

...
...
locationManager = CLLocationManager()
switch CLLocationManager.authorizationStatus() {
  case .AuthorizedAlways:
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.startUpdatingLocation()
  case .NotDetermined:
    locationManager.requestAlwaysAuthorization()
  case .AuthorizedWhenInUse, .Restricted, .Denied:
    mvc.alertDenied();
}

if (!initialized)
{
  initialized = true;
  self.performSelector("finishUpdatingLocation", withObject: nil, afterDelay: 2.0);
}

}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    print ("didUpdateLocations")
    var userLocation:CLLocation = locations[0] as! CLLocation
    longitude = Float(userLocation.coordinate.longitude);
    latitude = Float(userLocation.coordinate.latitude);
  }


 func finishUpdatingLocation () {
    locationManager.stopUpdatingLocation();
    NSObject.cancelPreviousPerformRequestsWithTarget(self, selector: "finishUpdatingLocation", object: nil);
    mvc.goingToUpdateHeading();
    locationManager.startUpdatingHeading();
}

正在调用 didUpdateLocations,我正在成功获取设备的坐标。然而,尽管我添加了 didUpdateHeading,但它的第一行没有被打印,设备卡在了这一点:

func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
print("didUpdateHeading");

if (newHeading.headingAccuracy > 0) {
  variation = newHeading.trueHeading - newHeading.magneticHeading;
  doneLoading = true;
  locationManager.stopUpdatingHeading();
  mvc.goingToCalculateData();

  if (calcData) {
    calculateData();
    print ("Calculating data");
    calcData = false;
  }
}
print(newHeading.magneticHeading)
}

为什么没有调用 startUpdatingHeading 有什么想法吗?

最佳答案

你必须像这样调用 locationManager.startUpdatingHeading()

...
...
locationManager = CLLocationManager()
switch CLLocationManager.authorizationStatus() {
  case .AuthorizedAlways:
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.startUpdatingLocation()
    locationManager.startUpdatingHeading()
  case .NotDetermined:
    locationManager.requestAlwaysAuthorization()
  case .AuthorizedWhenInUse, .Restricted, .Denied:
    mvc.alertDenied();
}

关于swift - 没有 swift 调用 didUpdateHeading,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35834700/

相关文章:

ios - 通过每个应用程序的扩展创建单例实例?

尝试获取设备位置时 iOS 应用程序崩溃 - 线程 1 : EXC_BAD_ACCESS

swift - 在 Swift 中设置计时器

iOS场景包: rotate node to look at another node

ios - 将文本字段导出集合添加在一起

ios - 无法在 Xcode 中的 View Controller 中移动按钮

ios - iOS 9 的 NSURLSession/NSURLConnection HTTP 加载失败和其他 AdMob 警告

ios - 推送通知到达时如何做一些工作?

ios - 无法确定用户何时不允许定位服务

swift - 在 Testflight beta 测试中进行测试时应用程序崩溃了