简体   繁体   English

iPhone 5上的CLLocationManager标题问题

[英]CLLocationManager heading issues on iPhone 5

I recently started testing my app on an iPhone 5, and to my alarm, it seems that the CLLocationManager doesn't really work! 我最近开始在iPhone 5上测试我的应用程序,令我震惊的是,似乎CLLocationManager并没有真正起作用! Although [CLLocationManager headingAvailable] is YES , I don't receive any heading updates at all. 尽管[CLLocationManager headingAvailable]YES ,但我根本没有收到任何标题更新。 Strangely, on an iPhone 4, after 30 or so heading updates, locationManager:didUpdateToHeading: is no longer called. 奇怪的是,在iPhone 4上,标题更新了30个左右之后,不再调用locationManager:didUpdateToHeading: This issue is entirely new. 这个问题是全新的。 The location manager also returns negative numbers for verticalAccuracy , so I'm assuming the altitude it is invalid. 位置管理器还会为verticalAccuracy返回负数,因此我假设它的高度无效。 Here's how I'm creating the location manager: 这是我创建位置管理器的方法:

CLLocationManager* locationManager = [[CLLocationManager alloc] init];
if([locationManager respondsToSelector:@selector(disallowDeferredLocationUpdates)]) {
   [locationManager disallowDeferredLocationUpdates];
   [locationManager setPausesLocationUpdatesAutomatically:NO];
}
locationManager.headingOrientation = CLDeviceOrientationFaceUp;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.headingFilter = -1;
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[sharedSingleton setLocationManager:locationManager];

sharedSingleton is just my singleton class that handles some odds and ends, including holding onto a reference to the location manager. sharedSingleton只是我的单例类,它处理一些零碎的事情,包括保留对位置管理器的引用。

If I need to post any more code let me know. 如果我需要发布更多代码,请告诉我。 I just don't know what might be causing this strange issue. 我只是不知道是什么原因导致这个奇怪的问题。 Thanks! 谢谢!

You need to retain " locationManager " in memory somewhere, either as a property of your object or as an instance variable. 您需要将“ locationManager ”保留在某个locationManager的内存中,作为对象的属性或实例变量。

What I belive is happening is that you're creating your location manager, and then your method exits and " locationManager " falls out of scope and is magically released by ARC. 我所相信的是,正在创建位置管理器,然后方法退出,“ locationManager ”超出范围,并由ARC神奇地释放了。

So, instead, do something like this: 因此,改为执行以下操作:

in your @implementation: 在您的@implementation中:

@property (strong) CLLocationManager * locationManager;

and in your @interface: 并在您的@interface中:

self.locationManager = [[CLLocationManager alloc] init];
if([self.locationManager respondsToSelector:@selector(disallowDeferredLocationUpdates)]) {
   [self.locationManager disallowDeferredLocationUpdates];
   [self.locationManager setPausesLocationUpdatesAutomatically:NO];
}
self.locationManager.headingOrientation = CLDeviceOrientationFaceUp;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.headingFilter = -1;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;

You could try a few things. 您可以尝试一些方法。 First of all, I don't see a startUpdatingHeading call. 首先,我看不到startUpdatingHeading调用。 Maybe you're doing it somewhere else. 也许您正在其他地方做。 You should add the locationManager:didFailWithError: method to the delegate to check for errors, and try returning YES in locationManagerShouldDisplayHeadingCalibration: in case it's a calibration issue. 您应该向locationManager:didFailWithError:添加locationManager:didFailWithError:方法以检查错误,并尝试在locationManagerShouldDisplayHeadingCalibration:中返回YES ,以防出现校准问题。

Seems the solution was obvious and I overlooked it. 似乎解决方案很明显,我却忽略了它。 The delegate was being set to nil just moments after the location manager was started, which explains why on a slower device like the iPhone 4 a few updates were able to come through before the code setting the delegate to nil was run, but on the iPhone 5 it was instantaneous. 在启动位置管理器后的瞬间,将委托设置为nil,这解释了为什么在运行速度慢的设备(如iPhone 4)上,可以在将委托设置为nil的代码运行之前通过一些更新,但是在iPhone上5它是瞬时的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM