指南针校准目标-c

我尝试在我的iOS应用程序中使用指南针。 我有一个问题。 如果我实现了locationManagerShouldDisplayHeadingCalibration方法并return YES ,那么校准显示总是显示。 但我应该像苹果地图一样。 即有时需要显示校准显示。 当罗盘应该校准。

好吧,我不能留下评论,所以我想我应该留下一个答复,因为克劳德Houle的答复对我有用。

我正在使用Claude Houle的改进版。

 - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager{ if(!manager.heading) return YES; // Got nothing, We can assume we got to calibrate. else if(manager.heading.headingAccuracy < 0 ) return YES; // 0 means invalid heading, need to calibrate else if(manager.heading.headingAccuracy > 5 ) return YES; // 5 degrees is a small value correct for my needs, too. else return NO; // All is good. Compass is precise enough. } 

也想说Claude Houle所说的几乎实现了API文档,其中指出:

如果您从此方法返回“否”,或者在代理中为其提供实施,则“核心位置”不显示标题校准警报。 即使没有显示警报,当任何干扰磁场远离设备时,仍然可以自然进行校准。 但是,如果设备由于某种原因无法进行自我校准,则任何后续事件的标题Acucuracy属性中的值都将反映未校准的读数。

我使用下面的代码:

 @property (nonatomic, retain) CLHeading * currentHeading; // Value updated by @selector(locationManager:didUpdateHeading:) ... ... - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager{ if( !self.currentHeading ) return YES; // Got nothing, We can assume we got to calibrate. else if( self.currentHeading.headingAccuracy < 0 ) return YES; // 0 means invalid heading. we probably need to calibrate else if( self.currentHeading.headingAccuracy > 5 )return YES; // 5 degrees is a small value correct for my needs. Tweak yours according to your needs. else return NO; // All is good. Compass is precise enough. } 

一个更直接的解决scheme:

Objective-C的

 - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager { CLLocationDirection accuracy = [[manager heading] headingAccuracy]; return accuracy <= 0.0f || accuracy > 10.0f; } 

这利用了select器在零对象上执行的事实总是返回零,事实上精度永远不会有效,等于0.0f(即100%准确)。

迅速

由于引入了可选项,最简单的Swift解决scheme需要分支,并且看起来像这样:

 func locationManagerShouldDisplayHeadingCalibration(manager: CLLocationManager) -> Bool { if let h = manager.heading { return h.headingAccuracy < 0 || h.headingAccuracy > 10 } return true } 

请注意,我们正在寻找headingAccuracy ,苹果的文档指出:

此属性中的正值表示磁头性质报告的值与磁北实际方向之间的潜在误差。 因此,这个属性的值越低,标题越精确。 负值意味着报告的标题是无效的,这可能在设备未校准或受到局部磁场的强烈干扰时发生。

manager.heading是CLHeading。 这就是为什么manager.heading> 5会给出警告。 self.currentHeading.headingAccuracy> 5是真实的。

在我的iPhone6上,标题精度通常是25.0,所以只需返回YES,依靠iOS来确定何时显示校准屏幕似乎是最好的select。 使用headingAccuracy <0.0舍弃读数可防止使用“错误”的标题。