我试图仅依靠设备的磁力计来获取设备的磁北偏角(以度为单位)。这是我写的代码,但我只是得到 0 度。我做错了什么?
CMMotionManager *motionManager;
motionManager = [[CMMotionManager alloc] init];
[motionManager startDeviceMotionUpdates];
CMDeviceMotion *deviceMotion;
deviceMotion = [[CMDeviceMotion alloc] init];
while(!self.stopButtonPressed)
{
double x = motionManager.deviceMotion.magneticField.field.x;
double y = motionManager.deviceMotion.magneticField.field.y;
double degrees = asin(y/sqrt(pow(x, 2.0) + pow(y, 2.0))) * 180.0 / M_PI ;
int degreesRounded = (int)degrees;
NSLog(@"Degrees : %i", degreesRounded);
}