没有立即调用didUpdateHeading [英] didUpdateHeading not called in swift

查看:67
本文介绍了没有立即调用didUpdateHeading的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get the heading information of the device in order to use the difference between the magnetic and true heading to determine the declination for the user location. For this, I have a Helper class and my MainVc (mvc). In my helper class init method I do the following:

...
...
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,但其中的第一行并未打印出来,但设备仍卡在该位置:

didUpdateLocations is being called and I am successfully fetching the device's coordinates. However although I have added didUpdateHeading, the first line in it is not being printed, the device's stuck at that point:

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

Any ideas why the startUpdatingHeading is not being called?

推荐答案

您必须像这样呼叫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();
}

这篇关于没有立即调用didUpdateHeading的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆