Which iOS class/code returns the magnetic North?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 09:14:52

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.

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

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;
user2150072

You need to fuse sensors to mimic the apple applications. They use the gyro accelerometer and magnetometer, and advanced mathmatics you may or may not have learned to produce such clean results. Good luck. You probably won't be able mimic them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!