地图之CoreLocation

1. 在Info.plist文件中添加下面两句话
  NSLocationAlwaysUsageDescription —> 确定定位吗?亲  (或者改参数类型为BOOL类型 值为Y)
  请求的授权,除了可以在APP打开时允许定位服务,也可以在APP进入后台仍然可以使用定位服务(永久) --> 与上边一个可以二选一
  [_locationManager requestAlwaysAuthorization];
  NSLocationWhenInUseUsageDescripyion —>需要定位吗? (或者改参数类型为BOOL类型 值为Y)
  此方法请求的授权,仅限于用户在打开使用APP时允许使用系统的定位服务(在应用使用期间)
  [_locationManager requestWhenInUseAuthorization];

2. 配置好后代码如下:

  1 #import "ViewController.h"
  2 #import "CoreLocation/CoreLocation.h"
  3 
  4 @interface ViewController () <CLLocationManagerDelegate>{
  5     CLLocationManager *_locationManager;
  6 }
  7 
  8 @end
  9 
 10 @implementation ViewController
 11 
 12 - (void)viewDidLoad {
 13     [super viewDidLoad];
 14     
 15     self.mySwitch.on = NO;
 16     //给switch添加点击事件
 17     [self.mySwitch addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
 18     
 19 }
 20 
 21 
 22 - (void)valueChange:(UISwitch *)locationSwitch {
 23     
 24     if (locationSwitch.on == YES) {
 25         //开启定位服务
 26         //判断设备是否可以使用定位服务(如何判断 两个 一个是判断定位服务 另一个是方向指示服务)
 27         if ([CLLocationManager locationServicesEnabled] == NO || [CLLocationManager headingAvailable] == NO) {
 28             NSLog(@"定位设备不可用");
 29             //将switch开关设置为NO
 30             [locationSwitch setOn:NO animated:YES];
 31             //程序返回
 32             return;
 33         }
 34         //往下是设备可以用
 35         if (_locationManager == nil) {
 36             //初始化定位管理者对象
 37             _locationManager = [[CLLocationManager alloc] init];
 38             
 39             //做定位的先关操作(设置)
 40             //设置精度
 41             /*
 42              kCLLocationAccuracyBest;               最高精度
 43              kCLLocationAccuracyNearestTenMeters;   10米的精度(超过10米响应)
 44              kCLLocationAccuracyHundredMeters;      100米精度
 45              kCLLocationAccuracyKilometer;          1千米精度
 46              kCLLocationAccuracyThreeKilometers;    3千米精度
 47              设置精度时根据实际情况而定,如果精度越高,手机耗电量越大
 48              */
 49             _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
 50             
 51             //设置定位的距离筛选器此属性的作用是当前位置发生改变时,其移动的距离超过了筛选值,就会触发协议中的方法
 52             //这里设置单位是米  (1米) double类型
 53             _locationManager.distanceFilter = 1;
 54             //设置代理
 55             _locationManager.delegate = self;
 56             /*
 57              设置手机的当前方向
 58              CLDeviceOrientationUnknown = 0,        没有
 59              CLDeviceOrientationPortrait,           肖像(正常拿手机的方向)
 60              CLDeviceOrientationPortraitUpsideDown, 肖像倒过来
 61              CLDeviceOrientationLandscapeLeft,      向左
 62              CLDeviceOrientationLandscapeRight,     向右
 63              CLDeviceOrientationFaceUp,             面向上(手机平放)
 64              CLDeviceOrientationFaceDown            面向下
 65 
 66              */
 67             _locationManager.headingOrientation = CLDeviceOrientationPortrait;
 68             
 69             //请求用户授权,自从iOS8.0开始需要手动请求用户授权
 70             //取到当前设备的版本号 如果大于8.0就手动开启提示
 71             if ([UIDevice currentDevice].systemVersion.floatValue > 8.0) {
 72                 
 73                 //此方法请求的授权,仅限于用户在打开使用APP时允许使用系统的定位服务(在应用使用期间)
 74                 [_locationManager requestWhenInUseAuthorization];
 75                 //请求的授权,除了可以在APP打开时允许定位服务,也可以在APP进入后台仍然可以使用定位服务(永久)   --> 与上边一个可以二选一
 76                 [_locationManager requestAlwaysAuthorization];
 77                 
 78             }
 79             //启动定位服务
 80             [_locationManager startUpdatingLocation];
 81             //启动方向服务
 82             [_locationManager startUpdatingHeading];
 83             
 84         }
 85         
 86         
 87         
 88     }else {
 89         
 90         //关闭开关 关闭定位服务
 91         NSLog(@"关闭定位服务");
 92     }
 93     
 94     
 95     
 96 }
 97 
 98 #pragma mark - 定位服务方法 -
 99 //定位失败的方法
100 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
101     
102     NSLog(@"定位失败:%@",error);
103     
104 }
105 
106 //定位位置更新成功的方法
107 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
108     NSLog(@"位置更新成功");
109     NSLog(@"%@",locations);
110     
111     /*
112      数组里是上次调用方法过来的,所有的位置变化都会放入到这个数组里,只需要取最后一个移动位置即可
113      位置变化如果超过位置的米数就会调用一次此方法,数组里是每米的变化
114      */
115     CLLocation *lastLacation = [locations lastObject];
116     
117     //获取经纬度结构体 (重点)
118     CLLocationCoordinate2D coordinate = lastLacation.coordinate;
119     
120     CLLocationDegrees dLatitude = coordinate.latitude; //纬度
121     CLLocationDegrees aLongitude = coordinate.longitude; //经度
122     
123     NSLog(@"经度:%f 纬度: %f",dLatitude,aLongitude);
124     
125     self.myLable.text = [NSString stringWithFormat:@"经度:%f 纬度: %f",dLatitude,aLongitude];
126     
127     //横向偏移 (了解)
128     NSLog(@"横向偏移: %f",lastLacation.horizontalAccuracy);
129     //纵向偏移 (了解)
130     NSLog(@"纵向偏移: %f",lastLacation.verticalAccuracy);
131     
132     
133 }
134 
135 
136 //更新方向的方法
137 - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
138     NSLog(@"方向发生改变");
139     //打印磁头方向
140     NSLog(@"%.1f",newHeading.magneticHeading);
141     
142     
143     
144 }
145 
146 //了解
147 - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager {
148     
149     //返回YES 允许出现方向矫正界面 NO就是不允许
150     return YES;
151     
152 }
153 
154 
155 @end
原文地址:https://www.cnblogs.com/GJ-ios/p/5461387.html