WWDC 2016 Session笔记 - Session 716 Core Location最佳实践(Core Location Best Practices)

WWDC 2016 Session笔记 - Session 716 Core Location最佳实践(Core Location Best Practices)

了解如何为用户提供良好的位置感知体验,同时节省所有Apple平台的电力。了解使用核心位置的最佳做法,应用程序如何使用和管理循环和信标区域,延迟或暂停位置更新以及如何使用访问监控来确定用户感兴趣的位置。概述用户如何授权应用程序使用位置服务。

Core Location Best Practices

authorization API 授权API

when in use authorization

调用方法requestWhenInUseAuthorization

AlwaysAuthorization

调用方法requestAlwaysAuthorization

后台定位的相关配置

  1. 配置后台定位。需要在plist文件中添加。
  2. 设置允许系统进行定位。(allowsBackgroundLocatioUpdates)

when is your app considered in use?

app is in the foreground
has a blue bar
handling WatchConnectivity messages, from a foreground watchOS app

常用API

single location API.

只获取一次位置信息,调用方法

(void)requestLocation
注意,调用了上述方法后必须实现以下代理方法:

(void)locationManager:(CLLocationManager )manager didUpdateLocations:(NSArray<CLLocation *> )locations;
(void)locationManager:(CLLocationManager )manager didFailWithError:(NSError )error;

continuous location API.

持续获取用户位置信息,调用方法

(void)startUpdatingLocation
这里需要注意的是,如果你实现了以下这个代理方法

(void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations;

那么下面这个代理方法将不被回调,也就是说如果你需要同时获取newLocation和oldLocation,只要实现下面这个代理方法,而且不能实现上面所述代理方法。

(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation;

另外特别需注意的是:Remember to stop your location updates when you’re done,调用方法:

(void)stopUpdatingLocation;

automatic pausing.(支持自动停止)

例子:假设用户是使用一个运动跟踪应用程序,他们去跑步,但当他们回家,他们累了,他们只是想洗澡,他们忘记关闭位置更新服务了。如果这时候运动跟踪程序会一直跑,则会损耗不必要的电量,影响电池寿命。
为此,Core Location在iOS 6.0提供了自动停止功能,如遇发现这种情况,自动停止位置服务。
该功能由属性pausesLocationUpdatesAutomatically. (默认为YES)控制。当pausesLocationUpdatesAutomatically这个属性设置成YES时,如果设备有一段时间位置没有发生变化,那么系统就会自动停掉位置更新服务。

这里的一段时间是系统自动判定的,可以通过设置CLLocationManager下的一个属性:activityType来决定这个时间的长短。

CLActivityTypeOther = 1,
CLActivityTypeAutomotiveNavigation, // for automotive navigation
CLActivityTypeFitness, // includes any pedestrian activities
CLActivityTypeOtherNavigation // for other navigation cases (excluding pedestrian navigation), e.g. navigation for boats, trains, or planes

如该属性的值为 CLActivityTypeAutomotiveNavigation(即为导航类APP),判断时间就会长些;如为CLActivityTypeFitness(运动类APP),判断时间就会短一些。因此,我们可以根据自己开发的APP类型或者是使用场景来决定如何设置这个值。

Deferred Updates.(延迟更新)

默认地,定位服务的代理会每秒钟都更新一次位置,这样对电池的消耗量会特别地大。除了设置pausesLocationUpdatesAutomatically这个属性以外,iOS还提供了DeferredUpdates的机制。
调用方法:

(void)allowDeferredLocationUpdatesUntilTraveled:(CLLocationDistance)distance
timeout:(NSTimeInterval)timeout;

你可以设置让系统每隔多远或者每隔多长时间更新一次位置。注意是“或”的关系,满足一个就会更新。使用这个方法有很多要注意的地方:

  • desiredAccuracy必须设置成kCLLocationAccuracyBest
  • distanceFilter必须设置成kCLErrorDeferredDistanceFiltered
  • 必须能够使用GPS进行定位(而不仅仅是移动数据或者Wi-Fi)
  • 非常重要的一点,DeferredUpdates只会出现在设备进入低耗电量的状态,App运行在前台或者设备连接在Xcode上正在调试是不会触发的。(所以不可能在Debug的时候打印Log来检验,要调试的话,需要写一些Log存在本地的数据库)

好处:Sometimes we do this for power reasons.(低电量时节省电量)

region monitoring(区域监测)

主要用于监测固定区域内的定位变化;即使当app挂起时,也可以进行数据更新。现主要有以下两种监测方式:

  1. circular region monitoring (圆形区域监控)

通过 startMonitoring stopMonitoring 进行监测的开始和结束
例子:

// 1. 判断区域监听服务是否可用(定位服务是否关闭, 定位是否授权, 是否开启飞行模式)
 if ([CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) 
 {

     // 创建区域中心
     CLLocationCoordinate2D center = CLLocationCoordinate2DMake(29.12345, 131.23456);

     // 创建区域(指定区域中心,和区域半径)
     CLLocationDistance radius = 1000;

     // 判断区域半径是否大于最大监听区域半径,如果大于, 就没法监听
     if (radius > self.locationM.maximumRegionMonitoringDistance) {
         radius = self.locationM.maximumRegionMonitoringDistance;
     }
     CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:radius identifier:@"小码哥"];

     // 开始监听指定区域
     [self.locationM startMonitoringForRegion:region];
 }
 else
 {
     NSLog(@"区域监听不可用");
 }

回调代理方法

// 进去监听区域后调用(调用一次)
 -(void)locationManager:(nonnull CLLocationManager *)manager didEnterRegion:(nonnull CLRegion *)region
 {
     NSLog(@"进入区域---%@", region.identifier);
     [manager stopMonitoringForRegion:region];
 }

 // 离开监听区域后调用(调用一次)
 -(void)locationManager:(nonnull CLLocationManager *)manager didExitRegion:(nonnull CLRegion *)region
 {
     NSLog(@"离开区域---%@", region.identifier);
 }
  1. Beacon region monitoring(灯塔区域监控)

significant location change monitoring API.(显著的位置变化监测)

visit monitoring (访问监控,监测用户感兴趣的地方)

调用方法:startMonitoringVisits 开启
调用方法:stopMonitoringVisits 关闭

geocoding API(地理编码API)

支持两种转码:
converting an address into a coordinate (将一个地址转换成坐标)
converting coordinates into an address (将坐标转换为一个地址)

概述

As far as availability is concerned, all of our APIs are available on iOS.
以上所述API在IOS中全部支持

On macOS, you can use the standard location service, circular region monitoring, significant location change monitoring and geocoding.

On watchOS, we support the standard location service, and geocoding, and finally, on tvOS, you can use the single location API, and the geocoding API.
以上几种API的试用范围:

guide you toward the right API

针对不同的应用场景,建议使用不同类型的API,
1. 如果你在写一个健身应用,建议您使用延迟位置更新的连续定位API
2. 如果你在写导航活着频繁刷新界面的应用,建议使用连续定位API,但如果在这些应用中需要记录用户的位置信息,建议使用延迟更新位置信息的API。
3. 在大多数情况下,我们会建议使用单一位置API。
4. 在允许的情况下,强烈建议使用when_in_use 授权API。
5. to create your CLLocationManager on a thread that has a runloop.(必须在拥有runloop的线程中创建),现在,对于许多应用程序,主线程是唯一拥有runloop的线程。我们要保证CLLocationManager建立在主线程,
6. 你应该确保你的APP设置了最大值精度值desiredAccuracy.(100米精度,core location通常不需要打开为GPS为了满足你的位置要求)
7. 鼓励使用自动暂定功能,建议使用延迟更新。(We’ve tuned the automatic pausing algorithm to be fairly conservative.)

总结

  1. 使用requestlocation ,定位的级别 ,例如 100m 以内为单位的 会开启gps 辅助定位;3km的内定位,精确度 就会有所降低。
  2. 注意及时停止定位 ;
  3. 注意电量问题
  4. 合理使用后台定位模式
    区域性监测
    使用全局变量CLLocationManager
    监测会持续到手动结束为止
    当不明确定位授权时,请停止监测
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值