@interface ViewController () <CLLocationManagerDelegate> {
    //定位器管理者
    CLLocationManager *_locationManager;
}
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.mySwitch.on = NO;
    //给swicht添加事件
    [self.mySwitch addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];
}
 
 
- (void)valueChanged:(UISwitch *)locationSwitch {
    if (locationSwitch.on == YES) {
        //启动定位服务
        //判断设备是否可以使用定位服务 (如何判断 两个 一个是定位服务 另一个方向指示服务)
        if ([CLLocationManager locationServicesEnabled] == NO || [CLLocationManager headingAvailable] == NO) {
            NSLog(@"定位设备不可用");
            //将switch开关设置为NO
            [locationSwitch setOn:NO animated:YES];
            //程序返回
            return;
        }
        //定位牵扯到硬件所以模拟器无法使用
        //往下是设备可用
        
        if (_locationManager == nil) {
            //初始化定位管理者对象
            _locationManager = [[CLLocationManager alloc] init];
            
            //做定位的相关操作(设置)
            
            //设置精度
            /*
             kCLLocationAccuracyBest;              最高精度
             kCLLocationAccuracyNearestTenMeters;  10米精度
             kCLLocationAccuracyHundredMeters;     100米精度
             kCLLocationAccuracyKilometer;         1千米精度
             kCLLocationAccuracyThreeKilometers;   3千米精度
             设置精度时根据实际情况而定,如果精度越高,手机耗电量越大
             */
            _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
            //设置定位的距离筛选器 ,此属性的作用是当前位置方式改变时,其移动的距离超过了筛选值,就会触发协议中的方法
            //这里设置单位是米  (1米) double类型
            _locationManager.distanceFilter = 1;
            //设置代理
            _locationManager.delegate = self;
            
            /*
             设置手机的当前方法
             了解
             CLDeviceOrientationUnknown = 0,    没有
             CLDeviceOrientationPortrait,       肖像
             CLDeviceOrientationPortraitUpsideDown, 肖像倒过来
             CLDeviceOrientationLandscapeLeft,    向左
             CLDeviceOrientationLandscapeRight,   向右
             CLDeviceOrientationFaceUp,           面向上
             CLDeviceOrientationFaceDown          面向下
 
             */
            _locationManager.headingOrientation = CLDeviceOrientationPortrait;
            
            //请求用户授权,自从iOS8.0开始需要手动请求用户授权
            //取到当前设备的版本号 如果大于8.0就手动开启提示
            if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) {
                //此方法请求的授权,仅限于用户在打开使用APP时允许使用系统的定位服务(在应用使用期间)
                [_locationManager requestWhenInUseAuthorization];
                //请求的授权,除了可以在APP打开时允许定位服务,也可以在APP进入后台仍然可以使用定位服务 (永久)
//                [_locationManager requestAlwaysAuthorization];
            }
            
        }
        //启动定位服务
        [_locationManager startUpdatingLocation];
        //启动方向服务
        [_locationManager startUpdatingHeading];
        
        
    }else {
        //关闭开关 关闭定位服务
        NSLog(@"关闭定位服务");
    }
    
    
}
 
 
#pragma mark - 定位服务方法 -
 
//定位失败的方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"定位失败:%@",error);
}
//定位位置更新成功的方法
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    NSLog(@"位置更新成功");
    NSLog(@"%@",locations);
    /*
     数组里是上次调用方法过来的,所有的位置变化都放入到这个数组里,只需要取最后一个移动位置即可
     位置变化如果超过设置的米数就会调用一次此方法,数组里是每米的变化
     */
    CLLocation *lastLacation = [locations lastObject];
    
    //获取经纬度结构体 (重点)
    CLLocationCoordinate2D coordinate = lastLacation.coordinate;
    CLLocationDegrees dLatitude = coordinate.latitude; //纬度
    CLLocationDegrees dLongitude = coordinate.longitude; //经度
    NSLog(@"经度:%f 纬度:%f",dLongitude,dLatitude);
    
    self.myLabel.text = [NSString stringWithFormat:@"经度:%f 纬度:%f",dLongitude,dLatitude];
    
    //横向偏移 (了解)
    NSLog(@"横向偏移:%f",lastLacation.horizontalAccuracy);
    //纵向偏移
    NSLog(@"纵向偏移:%f",lastLacation.verticalAccuracy);
    
    
    
}
 
//更新方向的方法
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    
    NSLog(@"方向方法改变");
    //打印磁头方法
    NSLog(@"%.1f",newHeading.magneticHeading);
}
 
 
//(了解)
- (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager {
    //返回YES 允许出现方向矫正界面 NO就是不允许
    return YES;
}