精通iOS开发--第19章 Core Location 和 Map Kit 上

19 Core Location Map Kit 上


位置管理器、设置精度、设置距离筛选器、启动位置管理器、合理使用位置管理器、位置管理器委托、获取位置更新、使用CLLocation获取经度纬度、错误通知、开始构建Core Location、将移动路线展现在地图上、小结


前言:

    CoreLocation框架决定他的物理位置。Map Kit用于创建实时交互的地图来显示你想要设备显示的任何位置。

    Core Location利用3种技术来实现该功能:GPS、蜂窝基站ID定位和WPS

    Core Location服务非常耗电,尽量只在必要的时候进行定位。在指定绝对最低精度级别时要谨慎,以避免不必要的电力消耗。

    Core Location所依赖的技术对于应用来说是隐藏的。我们不需要指定Core Location是使用GPS、蜂窝基站ID定位还是WPS,只是指定精度级别,然后他将自动从可用的技术中选择。

    

19.1 位置管理器

    位置管理器:Location ManagerCLLocationManager locationManager = [[CLLocationManager alloc] init]; 实例化一个位置管理后,并不会立即开始位置轮询。


19.1.1 设置精度

19.1.2 设置距离筛选器

19.1.3 启动位置管理器

19.1.4 合理使用位置管理器


19.2 位置管理器委托

19.2.1 获取位置更新

19.2.2 使用CLLocation获取经度纬度

19.2.3 错误通知


19.3 开始构建Core Location


19.4 将移动路线展现在地图上


19.5 小结


原来iOS8.0以后新的授权方式:


- (void)beginUpdatingLocation

{

    if ([CLLocationManager locationServicesEnabled])

    {

        self.locationManager = [[CLLocationManager alloc] init];

        /*

         iOS 8.0下要授权,iOS8以后采用了新的授权方式,需要再Info.plist中注册提示的内容。

         */

        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

        {

            // NSLocationAlwaysUsageDescription

            [self.locationManager requestAlwaysAuthorization];

            

            // NSLocationWhenInUseUsageDescription // authorization |ˌɔːθəraɪˈzeɪʃn| noun 授权、批准

            //[self.locationManager requestWhenInUseAuthorization];

        }

    }

    

    self.locationManager.delegate = self;

    

    // desiredAccuracy是一个double类型,代表mkCLLocationAccuracyBest表示提供最好的精度。

    // desire |dɪˈzaɪə(r)| nounv 渴望

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    

    // 默认情况下,位置管理器会把检测到的位置更改通知给委托。制定距离筛选器意味着告知位置管理器不要将更改都通知你,仅当位置更改超过特定大小时通知你。设置距离筛选器可以减少应用执行的轮询数量。代表米m

    // filter |ˈfɪltə(r)| n 过滤器

    self.locationManager.distanceFilter = 20;

    

    // 设置成kCLDistanceFilterNone后将取消前面的distanceFilter相关的设置。恢复到没有筛选器的状态。

    // self.locationManager.distanceFilter = kCLDistanceFilterNone;


    self.locationManager.activityType = CLActivityTypeFitness;


    

    self.locationManager.allowsBackgroundLocationUpdates = YES;

    

    // 启动位置管理器。

    [self.locationManager startUpdatingLocation];

}


注意:

   Info.plist文件还要加上NSLocationWhenInUseUsageDescription或者requestWhenInUseAuthorization,Value为弹出提示框时显示的内容。



1iOS6之后,苹果开始加强保护用户隐私,在 Info.plist 文件中定义 Key提醒用户,提高用户允许定位的概率。


2:如果要后台定位,需要再Capabilities中的Background Modes中勾选Location updates打开后台模式。


3iOS8之后,苹果又进一步加强了隐私保护,不会主动弹出对话框,需要实现两个方法(实现其一即可),并且 Info.plist 中设置对应的 key ,才会弹出提示框。

3.1requestWhenInUseAuthorization

1.当程序当前的授权状态为未决定时,在前台时请求定位服务许可时使用。需要先在Info.plist文件中设置一个Key:NSLocationWhenInUseUsageDescription, 如果不设置key,系统会忽略定位请求。

2.当用户授权 when-in-use时,程序在前台时可以启动大部分定位服务。如果想要后台定位,需要开启后台定位模式,但在状态栏会出现蓝条提示用户程序正在进行定位。

Pasted Graphic.tiff

3.2requestAlwaysAuthorization,需要 Info.plist 文件中包含key NSLocationAlwaysUsageDescription

3.3iOS8之后,如果想要定位,必须调用 requestWhenInUseAuthorization requestAlwaysAuthorization方法。


4:判断是否开启了定位服务,在启动更新位置之前要先判断是否开启了定位服务if ([CLLocationManager locationServicesEnabled]) {}


5:代理方法返回的 locations 信息;当位置管理器,获取到位置后,调用 locationManager:didUpdateLocations:方法,返回的类型为 CLLocation 的位置信息数组,以下为CLLocation包含的属性:1.coordinate : 当前位置的坐标、latitude : 纬度、longitude : 经度2.altitude : 海拔,高度、3.horizontalAccuracy : 纬度和经度的精度、4.verticalAccuracy : 垂直精度(获取不到海拔时为负数)5.course : 行进方向(真北)6.speed : 以米/秒为单位的速度、7.description : 位置描述信息。


6iOS9.0之后有一种新的请求定位的方法 requestLocation:按照定位精确度从低到高进行排序,逐个进行定位。如果获取到的位置不是精确度最高的那个,也会在定位超时后,通过代理告诉外界。需要注意的是:必须实现 locationManager: didUpdateLocations: locationManager: didFailWithError 方法,同时不能与startUpdatingLocation同时使用


7iOS90.0:如果当前的授权状态是使用是授权,那么App退到后台后,将不能获取用户位置,即使勾选后台模式:location,必须要设置 allowsBackgroundLocationUpdates 属性为YES(默认是NO)才可以。


Pasted Graphic_1.tiff


capability |ˌkeɪpəˈbɪləti| noun 能力、才能//streetcar noun American 有轨电车//Background ModesLocation updates//Add the “Required Backgrounds Modes” key to your info plist file.




1:后台实时上报地理位置:参考:http://blog.csdn.net/sxfcct/article/details/48519937 或者 http://blog.sina.com.cn/s/blog_672af4e20100wp5l.html 或是使用第三方库:http://www.cnblogs.com/oshushu/articles/4569252.html 建议参考:http://www.jianshu.com/p/174fd2673897

2:在 iOS 中,有“中国区地图校正”插件可以纠偏??

3:当程序在前台运行时,我们 可以从一个 CLLocationManager实例获得委托消息,在 iOS检测到设备移动到新位置时告诉你这一变化。当我们的 程序被送到后台并且不再处于活动状态,这些位置委托消息一般是不会被发给你的程序的。实际上,他们会在你的程序回到前台后,批量的被发送。这种说法是真的么????



/*

 *  startMonitoringSignificantLocationChanges

 *

 *  Discussion:

 *      Start monitoringmonitor |ˈmɒnɪtə(r)| n 监视器 v 监视 significantsignificant |sɪgˈnɪfɪkənt| adjective 重要的 location changes.  The behavior of this service is not affected by the desiredAccuracy or distanceFilter properties.  Locations will be delivered through the same delegate callback as the standard location service.

 *

 */

- (void)startMonitoringSignificantLocationChanges


/*

 * pausesLocationUpdatesAutomatically

 *

 *  Discussion:

 * Specifies that location updates may automatically be paused when possible. By default, this is YES for applications linked against iOS 6.0 or later.

 */

@property(assign, nonatomic) BOOL pausesLocationUpdatesAutomatically



/*

 * activityType

 *

 *  Discussion:

 * Specifies the type of user activity. Currently affects behavior such as the determination|dɪˌtɜːmɪˈneɪʃn| n 决心 of when location updates may be automatically paused.

 * By default, CLActivityTypeOther is used.

 */

@property(assign, nonatomic) CLActivityType activityType





/*

 *  distanceFilter

 *  

 *  Discussion:

 *      Specifies the minimum update distance in meters. Client will not be notified of movements of less than the stated(n 状态 v 陈述、说明) value, unless the accuracy has improvedimprove |ɪmˈpruːv| v 改善. Pass in kCLDistanceFilterNone to be notified of all movements. By default, kCLDistanceFilterNone is used.

 */

@property(assign, nonatomic) CLLocationDistance distanceFilter;




/*

 *  allowsBackgroundLocationUpdates

 *

 *  Discussion:

 *      By default, this is NO for applications linked against iOS 9.0 or later,regardless of minimum deployment target.

 *

 *      With UIBackgroundModes set to include "location" in Info.plist, you must also set this property to YES at runtime whenever calling -startUpdatingLocation with the intent|ɪnˈtent| 意 to continue in the background.

 *

 *      Setting this property to YES when UIBackgroundModes does not include "location" is a fatal|ˈfeɪtl| adj 致命的 error.

 *

 *      Resetting this property to NO is equivalent|ɪˈkwɪvələnt| n 想等物 adj 想等的 to omittingomit |əˈmɪt| v "location" from the UIBackgroundModes value.  Access to location is still permittedpermit |ˈpɜːmɪt| noun 许可证 whenever the application is running (ie not suspended), and has sufficientsufficient |səˈfɪʃnt| adjective 充足的 authorization (ie it has WhenInUse authorization and is in use, or it has Always authorization).  However, the app will still be subject(n 主题 vt 使臣服 vi取决于) to the usual task suspension|səˈspenʃn| n 停、挂起 rules.

 *

 *      See -requestWhenInUseAuthorization and -requestAlwaysAuthorization for more details on possible authorization values.

 */

@property(assign, nonatomic) BOOL allowsBackgroundLocationUpdates

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值