读书笔记---指南针与磁力计

主要的应用还是在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>


检测磁场有2种方式可以使用,一种是CLLocationManager与CMMotionManager

- (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];
}

个人认为这章与上一张还是类似,基本上就是告诉你了这个东西是怎么回事,如果想深入其实还是需要google,同时我也觉得,如果想要更深入的了解加速度计,陀螺仪,指南针,磁力计,也需要我们去了解相关的专业知识,以及一些数学知识,才能更灵活的运用于商业,个人认为指南针那个例子,是个很好的例子,如果有需要可以深入学习.


更多干货,请支持原作:http://item.jd.com/11436547.html



你可能感兴趣的:(ios,读书笔记,指南针,磁力计)