哪个iOS类/代码返回磁北? [英] Which iOS class/code returns the magnetic North?

查看:132
本文介绍了哪个iOS类/代码返回磁北?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让设备偏离磁北角度,并在我写的一些代码中使用该值。

I want to get the device's deviation from the magnetic North in degrees, and use that value in some code I'm writing.

我不想使用设备的位置服务,因此我对获得True North而不是Magnetic North(仅使用设备的磁力计)不感兴趣。

I don't want to use the device's location services and therefore I'm not interested in getting the True north but rather the Magnetic North (using only the device's magnetometer).

哪个类(或编码过程..)可以为我提供该值(仅依靠磁力计)?

Which class (or coding process.. ) could provide me with that value (solely relying on the magnetometer) ?

CLLocationManager类及其属性依赖于启用/可用的位置服务

the CLLocationManager class and its properties rely on Location Services being enabled/available

其中带有CMMagnetometerData类的Core Motion框架为我们提供了以下属性:

where as the Core Motion framework with its CMMagnetometerData class provides us with the following property:

@property(readonly, nonatomic) CMMagneticField magneticField

A structure containing 3-axis magnetometer data

typedef struct {
   double x;
   double y;
   double z;
} CMMagneticField;

如何获得学位?或者是否有其他方法(类/属性/方法)单独从磁力计中取出度数?

How do I get degrees out of that? or is there some other way (class/property/method) for getting degrees out of the magnetometer solely ?

提前感谢任何有关此问题的有用信息的人! :)

Thank you in advance to anyone with some helpful information on that matter! :)

推荐答案

iOS文档指出CMMagneticField数据是原始的,这意味着它包含从设备本身及其周围环境引入的偏差。 CMDeviceMotion 提供相同的磁场值过滤。

The iOS documentation states that the CMMagneticField data is raw, meaning that it includes bias introduced from the device itself and its surroundings. CMDeviceMotion provides the same magnetic field values filtered.

要确定磁北,你应该使用滤波值,设备应该与地球表面平齐。

To determine magnetic north you should use the filtered values and the device should lay level with Earth's surface.

知道磁场的x和y值角度(磁北偏角度)可以用以下公式

Knowing the x and y values of the magnetic field the angle (declination from magnetic north in degrees) can be calculated with the following formula:

if (y>0): heading = 90.0 - [arcTan(x/y)]*180/π
if (y<0): heading = 270.0 - [arcTAN(x/y)]*180/π
if (y=0, x<0): heading = 180.0
if (y=0, x>0): heading = 0.0

在Obj-C中,假设你有一个 CMMagnetometerData 名为 magnetometerData 的对象,如下所示:

In Obj-C, assuming you have a CMMagnetometerData object called magnetometerData, that would look something like:

 double heading = 0.0;
 double x = magnetometerData.magneticField.x;
 double y = magnetometerData.magneticField.y;
 double z = magnetometerData.magneticField.z;

 if (y > 0) heading = 90.0 - atan(x/y)*180.0/M_PI;
 if (y < 0) heading = 270.0 - atan(x/y)*180.0/M_PI;
 if (y == 0 && x < 0) heading = 180.0;
 if (y == 0 && x > 0) heading = 0.0;

这篇关于哪个iOS类/代码返回磁北?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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