CoreMotion框架

时间:2011-03-16 02:09:04

标签: compass-geolocation

我最近做了一个指南针,涉及CoreMotion框架,但是我查了很久没找到关于如何实现这个框架指南针效果的信息,希望得到大家的帮助。

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

我的“Helloworld”CoreMotion:

1)添加CoreMotion.framework

2)在.h文件中:

#import <CoreMotion/CoreMotion.h>

3)在.m文件中:

CMMotionManager *myMotionManager= [[CMMotionManager alloc] init];
myMotionManager.deviceMotionUpdateInterval = 1/60;
[myMotionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXMagneticNorthZVertical  toQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
     double x = myMotionManager.deviceMotion.magneticField.field.x;
     double y = myMotionManager.deviceMotion.magneticField.field.y;
     double z = myMotionManager.deviceMotion.magneticField.field.z;
    NSLog(@"Strength of Earth's magnetic field= %8.2f",sqrt(x*x+y*y+z*z));
    myLabel.text = [NSString stringWithFormat:@"%8.2f", sqrt(x*x+y*y+z*z)];
}];