主要的应用还是在CoreLocation框架下,直接上代码
<span style="font-size:18px;"> - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.locationManager = [[CLLocationManager alloc]init]; self.locationManager.delegate = self; if ([CLLocationManager locationServicesEnabled] && [CLLocationManager headingAvailable]) {//判断是否能获得航向数据 [self.locationManager startUpdatingLocation]; [self.locationManager startUpdatingHeading]; }else { NSLog(@"不能获得导航数据"); } } - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{ UIDevice *device = [UIDevice currentDevice]; if (newHeading.headingAccuracy > 0) { float magneticHeading = [self heading:newHeading.magneticHeading fromOrientation:device.orientation];//地磁北方向 float trueHeading = [self heading:newHeading.trueHeading fromOrientation:device.orientation];//地理北方向 self.magneticHeadingLabel.text = [NSString stringWithFormat:@"%.2f",magneticHeading]; self.trueHeadingLabel.text = [NSString stringWithFormat:@"%.2f",trueHeading]; float heading = -1.0f *M_PI *newHeading.magneticHeading/180.0f; self.plateImage.transform = CGAffineTransformMakeRotation(heading); } } //是否校验磁力计 - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager{ return YES; } //根据不同朝向的时候的角度修正 - (float)heading:(float)heading fromOrientation:(UIDeviceOrientation)orientation{ float realHeading = heading; switch (orientation) { case UIDeviceOrientationPortrait: break; case UIDeviceOrientationPortraitUpsideDown: realHeading = realHeading - 180.0f; break; case UIDeviceOrientationLandscapeLeft: realHeading = realHeading + 90.0f; break; case UIDeviceOrientationLandscapeRight: realHeading = realHeading - 90.0f; break; default: break; } //保证角度是取值在360度之间 if (realHeading >360.0f) { realHeading -=360; }else if (realHeading <0.0f){ realHeading +=360; } return realHeading; } </span>
- (void)viewDidLoad { [super viewDidLoad]; _locationManager = [[CLLocationManager alloc] init]; _locationManager.delegate = self; if( [CLLocationManager headingAvailable]) { [_locationManager startUpdatingHeading]; } else { NSLog(@"磁力计不可用。"); } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; [_locationManager stopUpdatingHeading]; _locationManager.delegate = nil; } - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)heading { NSLog(@"x=%.2f", heading.x); self.xLabel.text = [NSString stringWithFormat:@"%.2f", heading.x]; self.xBar.progress = ABS(heading.x/100); NSLog(@"y=%.2f", heading.y); self.yLabel.text = [NSString stringWithFormat:@"%.2f", heading.y]; self.yBar.progress = ABS(heading.y/100); NSLog(@"z=%.2f", heading.z); self.zLabel.text = [NSString stringWithFormat:@"%.2f", heading.z]; self.zBar.progress = ABS(heading.z/100); CGFloat magnitude = sqrt(heading.x*heading.x + heading.y*heading.y + heading.z*heading.z); self.magnitude.text = [NSString stringWithFormat:@"%.2f", magnitude]; }
- (void)viewDidLoad { [super viewDidLoad]; self.motionManager = [[CMMotionManager alloc] init]; self.motionManager.magnetometerUpdateInterval = 1/30; if ([self.motionManager isMagnetometerAvailable]){ [self.motionManager startMagnetometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMagnetometerData *magnetometerData, NSError *error) { if (error) { [self.motionManager stopMagnetometerUpdates]; } else { CMMagneticField heading= magnetometerData.magneticField; NSLog(@"x=%.2f", heading.x); self.xLabel.text = [NSString stringWithFormat:@"%.2f", heading.x]; self.xBar.progress = ABS(heading.x/1000); NSLog(@"y=%.2f", heading.y); self.yLabel.text = [NSString stringWithFormat:@"%.2f", heading.y]; self.yBar.progress = ABS(heading.y/1000); NSLog(@"z=.2%f", heading.z); self.zLabel.text = [NSString stringWithFormat:@"%.2f", heading.z]; self.zBar.progress = ABS(heading.z/1000); CGFloat magnitude = sqrt(heading.x*heading.x + heading.y*heading.y + heading.z*heading.z); self.magnitude.text = [NSString stringWithFormat:@"%.2f", magnitude]; } }]; } else { NSLog(@"磁力计不可用."); } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; [self.motionManager stopMagnetometerUpdates]; }
更多干货,请支持原作:http://item.jd.com/11436547.html