iPhone/iPad 中的指南针真实航向

发布于 2024-12-04 05:38:21 字数 663 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

路还长,别太狂 2024-12-11 05:38:21

为了避免标题不断返回-1.000000,不只是运行startUpdatingHeading,而是一起运行startUpdatingLocation,这会有所帮助。

to avoid the heading keep returning -1.000000, not JUST run startUpdatingHeading but run startUpdatingLocation together, this helps.

幻梦 2024-12-11 05:38:21

尝试使用这个...

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
    // Start heading updates. 
    if (locationManager.headingAvailable && locationManager.locationServicesEnabled) 
    {
        locationManager.headingFilter = kCLHeadingFilterNone; 
        [locationManager startUpdatingHeading];
    }

并且在执行此 CLLocationManager 委托方法调用之后

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {

    // Use the true heading if it is valid. 
    [lblAccuracy setText:[NSString stringWithFormat:@"%.1fmi",newHeading.headingAccuracy]]; 
}

但是此编码适用于设备而不是模拟器...

快乐编码..

Try using this...

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate=self;
    locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
    // Start heading updates. 
    if (locationManager.headingAvailable && locationManager.locationServicesEnabled) 
    {
        locationManager.headingFilter = kCLHeadingFilterNone; 
        [locationManager startUpdatingHeading];
    }

and after doing this CLLocationManager delegate methods calls

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {

    // Use the true heading if it is valid. 
    [lblAccuracy setText:[NSString stringWithFormat:@"%.1fmi",newHeading.headingAccuracy]]; 
}

But this coding works on device not in simulator...

Happy coding..

怕倦 2024-12-11 05:38:21

---我的解决方案---

我所做的是,将 [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.

咋地 2024-12-11 05:38:21

我自己在使用位置管理器时遇到了一些麻烦,发现对我来说,在测试时它有助于将 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.

北方的韩爷 2024-12-11 05:38:21

我也有同样的问题。我将 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.

手心的温暖 2024-12-11 05:38:21

真正的读数需要了解您使用指南针的地方的磁力变化。从前面的讨论来看,从磁方向校正真实方向的函数似乎需要您的位置来获取变化值。如果您不喜欢在代码中使用位置 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.

负佳期 2024-12-11 05:38:21

当位置服务关闭时,didUpdateHeading 委托方法仅返回磁航向。您可以根据您的需要使用它。根据苹果文档..

要开始传递与标题相关的事件,请将委托分配给
位置管理器对象并调用其 startUpdatingHeading 方法。
如果还启用了位置更新,位置管理器将返回
真航向和磁航向值。如果位置更新
未启用,位置管理器仅返回磁性
标题值。

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

To begin the delivery of heading-related events, assign a delegate to
the location manager object and call its startUpdatingHeading method.
If location updates are also enabled, the location manager returns
both the true heading and magnetic heading values. If location updates
are not enabled, the location manager returns only the magnetic
heading value.

亣腦蒛氧 2024-12-11 05:38:21

现在正在解决这个问题。当我使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文