Ios7 如何利用磁强计测量航向角的变化

Ios7 如何利用磁强计测量航向角的变化,ios7,core-motion,magnetometer,Ios7,Core Motion,Magnetometer,从现在开始,我一直在研究CoreMotion框架。我想得到与磁北相对应的角度,当用户拥有iOS设备时,向其他方向移动,例如,我有iOS设备,我目前正朝北,比如说,位置A,然后我转向并开始朝东位置B。现在我想要在与设备磁北相对应的朝北位置B中创建的角度 这就是我现在如何在谷歌搜索时找到某个地方 [_motionManager startMagnetometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMMagneto

从现在开始,我一直在研究CoreMotion框架。我想得到与磁北相对应的角度,当用户拥有iOS设备时,向其他方向移动,例如,我有iOS设备,我目前正朝北,比如说,位置A,然后我转向并开始朝东位置B。现在我想要在与设备磁北相对应的朝北位置B中创建的角度

这就是我现在如何在谷歌搜索时找到某个地方

[_motionManager startMagnetometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMMagnetometerData *magnetometerData, NSError *error)
         {
             double heading = 0.0;
             double x = magnetometerData.magneticField.x;
             double y = magnetometerData.magneticField.y;
             //double z = motion.magneticField.field.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;

             NSLog(@"Heading Angle = %f",heading);

         }];

提前感谢。

您找到的代码有什么问题?似乎是个数学问题。@Larme有什么办法消除这个错误吗?