1.iOS8曾经使用CLLocationManager

  • 1.导入头文件 <CoreLocation/CoreLocation.h>
  • 2.创建位置管理者 CLLocationManager , 并加入到属性。
  • 3.设置代理、遵守协议、实现代理方法,在代理方法中获取位置信息
  • 4.调用開始更新位置方法
  • 5.设置 每隔多远定位一次准确度。准确度越高越耗电,定位时间越长

    // 1.设置位置管理者属性
    @property (nonatomic, strong) CLLocationManager *lcManager;
    // 2.推断是否打开了位置服务
    if ([CLLocationManager locationServicesEnabled]) {
    // 创建位置管理者对象
    self.lcManager = [[CLLocationManager alloc] init];
    self.lcManager.delegate = self; // 设置代理
    // 设置定位距离过滤參数 (当本次定位和上次定位之间的距离大于或等于这个值时,调用代理方法)
    self.lcManager.distanceFilter = 100;
    self.lcManager.desiredAccuracy = kCLLocationAccuracyBest; // 设置定位精度(精度越高越耗电)
    [self.lcManager startUpdatingLocation]; // 開始更新位置
    }
    /** 获取到新的位置信息时调用*/
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
    NSLog(@"定位到了");
    }
    /** 不能获取位置信息时调用*/
    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
    NSLog(@"获取定位失败");
    }
  • 6.请求授权,iOS6之后,苹果開始加强保护用户隐私,在 Info.plist 文件里定义 Key提醒用户,提高用户同意定位的概率。

    imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" class="imagebubble-image" alt="">

    Info.plist 设置Key
  • 7.假设要后台定位。须要打开后台模式

    imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" class="imagebubble-image" alt="">

    勾选后台模式

2.iOS8.0之后使用CLLocationManager

  • 1.iOS8之后,苹果又进一步加强了隐私保护。不会主动填出对话框。让用户选择
  • 2.须要实现两个方法(实现其一就可以),而且 Info.plist 中设置相应的 key ,才会弹框
1.requestWhenInUseAuthorization
  • 1.当程序当前的授权状态为未决定时,在前台时请求定位服务许可时使用。须要先在 Info.plist 文件里设置一个Key:NSLocationWhenInUseUsageDescription, 假设不设置key。系统会忽略定位请求。

    Info.plist 设置相应的Key
  • 2.当用户授权 when-in-use时,程序在前台时能够启动大部分定位服务。假设想要后台定位,须要开启后台定位模式。但在状态栏会出现蓝条提示用户程序正在进行定位。

    [_lcManager requestWhenInUseAuthorization];

    imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" class="imagebubble-image" alt="">

    请求定位的弹框

imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" class="imagebubble-image" alt="">

后台模式下的 blue bar
2.requestAlwaysAuthorization
  • 1.请求前后台定位服务授权,当授权状态为未决定时请求用户授权。前提是在 Info.plist 文件里包括key NSLocationAlwaysUsageDescription

imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" class="imagebubble-image" alt="">

Info.plist中 设置相应的Key

请求定位的弹框
3.注意
  • 1.iOS8之后,假设想要定位。必须调用 requestWhenInUseAuthorizationrequestAlwaysAuthorization方法。
  • 2.假设两个请求授权的方法都运行了,会出现下面情况
    • 1.when-in-use写在前面,第一次打开程序时请求授权,假设勾选了后台模式,进入后台会出现蓝条提示正在定位。当程序退出,第二次打开程序时
      Always 会再次请求授权。之后进入后台就不会出现蓝条了(前后台都能定位)。
    • 2.Always写在前面, when-in-use写在后面。仅仅会在第一次打开程序时请求授权,由于
      Always
      得到的授权大于when-in-use的到的授权
4.推断是否开启了定位服务
  • 在启动更新位置之前要先推断是否开启了定位服务

    if ([CLLocationManager locationServicesEnabled]) { // 推断是否打开了位置服务
    [self.lcManager startUpdatingLocation]; // 開始更新位置
    }
5.适配版本的方法
  • when-in-useAlways 都是iOS8之后出现的方法。假设不进行版本号适配,执行在iOS7上就会crash,此时须要做版本号号推断
  • 1.推断版本号号
    if ([[UIDevice currentDevice].systemVersion floatValue] >=8.0 ) {
    [_lcManager requestAlwaysAuthorization];
    }
  • 2.适配版本号的还有一种方法
    if ([_lcManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [_lcManager requestWhenInUseAuthorization];
    }
6.监听定位服务状态的改变
  • 实现代理方法。推断定位服务的状态

    /** 定位服务状态改变时调用*/
    -(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
    {
    switch (status) {
    case kCLAuthorizationStatusNotDetermined:
    {
    NSLog(@"用户还未决定授权");
    break;
    }
    case kCLAuthorizationStatusRestricted:
    {
    NSLog(@"訪问受限");
    break;
    }
    case kCLAuthorizationStatusDenied:
    {
    // 类方法,推断是否开启定位服务
    if ([CLLocationManager locationServicesEnabled]) {
    NSLog(@"定位服务开启,被拒绝");
    } else {
    NSLog(@"定位服务关闭,不可用");
    }
    break;
    }
    case kCLAuthorizationStatusAuthorizedAlways:
    {
    NSLog(@"获得前后台授权");
    break;
    }
    case kCLAuthorizationStatusAuthorizedWhenInUse:
    {
    NSLog(@"获得前台授权");
    break;
    }
    default:
    break;
    }
    }
7.代理方法返回的 locations 信息
  • 当位置管理器。获取到位置后,调用 locationManager:didUpdateLocations:方法,返回的类型为
    CLLocation
    的位置信息数组,下面为数组包括的属性

    • 1.coordinate : 当前位置的坐标

      • latitude : 纬度
      • longitude : 经度
    • 2.altitude : 海拔。高度
    • 3.horizontalAccuracy : 纬度和经度的精度
    • 4.verticalAccuracy : 垂直精度(获取不到海拔时为负数)
    • 5.course : 行进方向(真北)
    • 6.speed : 以米/秒为单位的速度
    • 7.description : 位置描写叙述信息
      -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
      {
      CLLocation *location = [locations firstObject];
      NSLog(@"%@", location);
      }

3.iOS9.0之后使用CLLocationManager

  • 1.iOS9.0之后有一种新的请求定位的方法 requestLocation
  • 2.作用:依照定位准确度从低到高进行排序,逐个进行定位。

    假设获取到的位置不是准确度最高的那个,也会在定位超时后。通过代理告诉外界。

  • 3.注意:
    • 1.必须实现 ocationManager:didUpdateLocations:locationManager:didFailWithError 方法,可是仅仅调用一次
    • 2.不能与startUpdatingLocation同一时候使用
      if ([CLLocationManager locationServicesEnabled]) { // 推断是否打开了位置服务
      [self.lcManager requestLocation];
      }
  • 4.实现 requestWhenInUseAuthorizationrequestAlwaysAuthorization 方法,并设置相应的 key

    if ([[UIDevice currentDevice].systemVersion floatValue] >=8.0 ) {
    // iOS0.0:假设当前的授权状态是使用是授权,那么App退到后台后。将不能获取用户位置,即使勾选后台模式:location
    [_lcManager requestWhenInUseAuthorization];
    }
  • 5.必须勾选后台模式,并设置 allowsBackgroundLocationUpdates 属性为YES(默认是NO)

    • 1.当定位完毕时。设置为NO,而且不再定位跟踪
    • 2.使用 -responsdToSelector: 推断

      // iOS9.0+ 要想继续获取位置。须要使用下面属性进行设置(注意勾选后台模式:location)但会出现蓝条
      if ([_lcManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
      _lcManager.allowsBackgroundLocationUpdates = YES;
      }

美味不用等在做定位时:在入口类,运行以下代码。应以每次启动时依据用户已经定位设置的状态来处理

if ([CLLocationManager
locationServicesEnabled]) {

switch ([CLLocationManager
authorizationStatus]) {

case
kCLAuthorizationStatusNotDetermined:

case
kCLAuthorizationStatusRestricted:{

[[NWLocationManager
sharedInstance].locationManager
requestWhenInUseAuthorization];//IOS8以后必需要调用此方法才干够定位

}

break;

case
kCLAuthorizationStatusDenied:{

[[SCLocationManager
sharedInstance] showSettingAlertView];

}

break;

case
kCLAuthorizationStatusAuthorizedAlways:

case
kCLAuthorizationStatusAuthorizedWhenInUse:{

}

default:

break;

}

[[NWLocationManager
sharedInstance].locationManager
startUpdatingLocation];//開始定位

}else{

NWLog(@"Alert:
定位服务不可用");

}

最新文章

  1. python __call__ 内置函数的使用
  2. document.body / document.ducumentElement /等获取高度和宽度的区别 ----转载
  3. Eclipse下Android开发的问题:Failed to install AndroidPhone.apk on device &#39;emulator-5554&#39;: timeout 解决办法
  4. SQL2005删除复制数据库的发布与订阅的方法(转载)
  5. NSAutoreleasePool&#39; is unavailable: not avail
  6. 深入了解jquery中的键盘事件
  7. Laravel 4 Blade模板引擎
  8. 解决的方法:warning: Clock skew detected. Your build may be incomplete.
  9. yum groupinstall &quot;Development Tools&quot; 批量安装软件
  10. IOS8 : UIAlertController
  11. B/S 和 C/S两种架构
  12. Spring邮件发送1
  13. centos7防火墙导致不能访问的
  14. 多重线性回归 (multiple linear regression) | 变量选择 | 最佳模型 | 基本假设的诊断方法
  15. 日志记录模块logging
  16. poj-2001(字典树)
  17. js parseInt()与Number()区别
  18. 小程序的wx.onAccelerometerChange
  19. metamask-iframe-stream,没成功
  20. winform 给textbox 增加 或 减小字体大小 z

热门文章

  1. 【bzoj3944/bzoj4805】Sum/欧拉函数求和 杜教筛
  2. linux下编译libmysqlclient, 安装mysql-server mysql-client
  3. 小米监控 open-falcon部署
  4. python(5)-- 函数
  5. 【11】 Express安装入门与模版引擎ejs
  6. 【01】npm/cnpm安装
  7. Bzoj2829 信用卡凸包
  8. ToolTip特效 JavaScript 盗取厦门人才网的特效
  9. 阿里巴巴Java开发手册公开版(转)
  10. 长沙理工校赛I题题解-连续区间的最大公约数