【问题标题】:didUpdateHeading not called in swift没有迅速调用 didUpdateHeading
【发布时间】:2016-03-07 01:13:05
【问题描述】:

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

...
...
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 吗?

【问题讨论】:

    标签: swift ios9 cllocationmanager


    【解决方案1】:

    您必须致电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();
    }
    

    【讨论】:

    • 我正在调用它,我停止更新位置后第一块代码的最后一行!
    【解决方案2】:

    如果你是在模拟器上检查,那么它不会工作,请在真机上检查。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-28
      相关资源
      最近更新 更多