iPhone/iPad 中的指南针真实航向
我在 iPhone/iPad 指南针开发上遇到一些问题。
从 CLHeading 中获取的 trueHeading 总是给我“-1”值,我被困在这里。这是我的代码:
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
self.locationManager.headingFilter = 0.5; //1 degrees
[self.locationManager startUpdatingHeading];
我还发现了一些事情,那就是当我在地图应用程序或指南针应用程序上使用位置信息时,trueHeading 值突然读取正确。我想知道是什么原因,有什么想法吗? iPhone4 和 iPad 上都会出现这种情况。
每当我在设置中关闭位置服务并重新启用它时,也会发生这种情况,它将无法读取正确的 trueHeading 值,我想知道是因为我创建的应用程序无法启用位置服务吗?
无论如何,提前感谢
---我的解决方案---
见下文。
I having some problem on the iPhone/iPad compass development.
The trueHeading taken from the CLHeading alway give me the '-1' value, I'm stuck here. Here is my code:
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
self.locationManager.headingFilter = 0.5; //1 degrees
[self.locationManager startUpdatingHeading];
I also found out something, that is when I on the map app or the compass app which has use the location stuff, the trueHeading value suddenly read correct. I wonder what is the cause, any idea? It happen on both iPhone4 and on the iPad.
It also happen whenever I off the Location Services in settings and re-enable it, it will become unable to read the correct trueHeading value, i wonder because the location services cannot be enable by the app I creating?
anyway, thank in advance
---My Solution---
see below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
为了避免标题不断返回-1.000000,不只是运行startUpdatingHeading,而是一起运行startUpdatingLocation,这会有所帮助。
to avoid the heading keep returning -1.000000, not JUST run startUpdatingHeading but run startUpdatingLocation together, this helps.
尝试使用这个...
并且在执行此 CLLocationManager 委托方法调用之后
但是此编码适用于设备而不是模拟器...
快乐编码..
Try using this...
and after doing this CLLocationManager delegate methods calls
But this coding works on device not in simulator...
Happy coding..
---我的解决方案---
我所做的是,将
[self.locationManager startUpdatingLocation]
添加到[self.locationManager startUpdatingHeading] 之前或之后;
(当定位服务关闭并从“设置”重新启用时)。我不确定这是否是一个好的解决方案,但这就是我为使其发挥作用所做的,如果您有更好的解决方案,请分享。---My Solution---
What I did was, add in the
[self.locationManager startUpdatingLocation]
to before or after the[self.locationManager startUpdatingHeading];
(when Location Services is off & re-enable from the Settings). I'm not sure this is a good solution, but this is what I did to make it work, if you have any better solution please share.我自己在使用位置管理器时遇到了一些麻烦,发现对我来说,在测试时它有助于将 iPhone 从计算机上拔下。不知何故,只有在拔掉 iPhone 插头后才会弹出校准警报。
I had some trouble with the location manager myself and found out that for me it helped to unplug the iPhone from the computer when testing. Somehow the calibration alert only popped up after unplugging the iPhone.
我也有同样的问题。我将 startUpdatingHeading 移至按钮操作中,然后将其移回分配 CLLocationManager 的位置(它一直工作正常的位置),然后它开始仅返回 -1。
我重新启动了 iPad,它又开始工作了。希望它能保持这种状态。
编辑:不,它并没有保持这种状态。我也必须使用 startUpdatingLocation 。但这不会消耗电池吗?我将desiredAccuracy 设置为kCLLocationAccuracyThreeKilometers,因为我无论如何都没有使用位置数据。
I had this same problem. I moved startUpdatingHeading into a button action, then moved it back to where the CLLocationManager is allocated -- where it had been working fine -- and it started returning only -1.
I rebooted my iPad and it started working again. Hopefully it stays that way.
Edit: Nope, it didn't stay that way. I had to use startUpdatingLocation too. Won't this wear down the battery though? I set desiredAccuracy to kCLLocationAccuracyThreeKilometers, because I am not using location data anyway.
真正的读数需要了解您使用指南针的地方的磁力变化。从前面的讨论来看,从磁方向校正真实方向的函数似乎需要您的位置来获取变化值。如果您不喜欢在代码中使用位置 GPS 信息,我建议您读取磁力读数并自行更正该值。您需要首先获得所需位置的变化,然后应用以下公式:T=M ± V,其中 T 是真实方向,M 是罗盘磁读数,V 是变化。使用“+”代表东,“-”代表西。我发现允许的网站为任何需要的位置提供变化(磁偏角): http://www.geomag.nrcan.gc.ca/calc/mdcal-eng.php。
A TRUE reading requires knowing the magnetic variation for the place where you are using the compass. From the previous discussion, it appears to be that the function that corrects the true direction from magnetic direction needs your location for obtaining the variation value. If you don't like to use the location GPS information in your code, I suggest reading the magnetic reading and correct the value by yourself. You need to obtain the variation for the desired location first then apply the following formula: T=M ± V, where T is the true direction, M is the compass magnetic reading and V is the variation. Use "+" for East and "-" for West. I found the allowing web site provide the variation(magnetic declination) for any needed location: http://www.geomag.nrcan.gc.ca/calc/mdcal-eng.php.
当位置服务关闭时,
didUpdateHeading 委托方法
仅返回磁航向。您可以根据您的需要使用它。根据苹果文档..When location services are off, the
didUpdateHeading delegate method
returns only the magnetic heading. You can use it according to your needs. According to Apple docs..现在正在解决这个问题。当我使用 SpriteKit 时,我可以从 Core Motion 获取更新。它是关于能够连续调用一个函数,例如每帧(1/60 秒)或每隔几帧调用一次。在不使用 SpriteKit 的情况下,文档说要在闭包中调用更新,我认为这将在它们自己的线程上并由您来发布。
有一种算法可以将磁力计读数转换为相对于真北的实际度数。想象一个看起来像交流电时域函数的图表,您会发现对数据进行插值是应用麦克斯韦方程组的简单问题。 这是一个关于 Honeywell 的示例
Working on this problem now. I can get updates from Core Motion when I use SpriteKit. It's about being able to call a function continuously like once a frame (1/60th of a second) or every few frames. Without using SpriteKit, the documentation says to invoke the updates within a closure, which I assume will be on their own thread and up to you to release.
There's an algorithm for converting the magnetometer readings to actual degrees relative to true north. Picture a graph that looks like the time domain function of alternating current and you'll see that interpolating the data is a simple matter of applying Maxwell's equations. Here's an example on honeywell