IOS-corelocation

来源:互联网 发布:javascript设计模式pdf 编辑:程序博客网 时间:2024/06/05 19:31

尽管有很多的第三方框架用于定位,但是都是建立在苹果的corelocation的基础之上的。

刚刚学习了corelocation,在此就记录一下。
第一步:你要导入CLLocationManager头文件
第二步:拥有CLLocationManager对象(进行懒加载即可)

- (CLLocationManager *)mgr{    if(!_mgr){        _mgr = [[CLLocationManager alloc] init];    }    return _mgr;}

设置mgr的代理,遵守协议

self.mgr.delegate = self;

第三步:主动请求,要求用户授权
调用对象方法

[mgr requestAlwaysAuthorization] //始终[mgr requestWhenInUseAuthorization] //可见

然后在info文件中添加NSLocationAlwaysUsageDescription/NSLocationWhenInUsesDescription(始终/可见)项。

第四步:授权状态改变通知代理方法,成功后开始监听

(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{    /*    // CLAuthorizationStatus    用户从未选择过权限    kCLAuthorizationStatusNotDetermined    无法使用定位服务,该状态用户无法改变    kCLAuthorizationStatusRestricted    用户拒绝该应用使用定位服务,或是定位服务总开关处于关闭状态    kCLAuthorizationStatusDenied    已经授权(废弃)    kCLAuthorizationStatusAuthorized    用户允许该程序无论何时都可以使用地理信息    kCLAuthorizationStatusAuthorizedAlways    用户同意程序在可见时使用地理位置    kCLAuthorizationStatusAuthorizedWhenInUse    */    if(status == kCLAuthorizationStatusAuthorizedWhenInUse ||kCLAuthorizationStatusAuthorizedAlways){        [mgr startUpdatingLocation];    }}

第五步:调用监听代理方法
didUpdateLocation;
在代理方法的参数中,locations是location对象的数组,location对象包含了各种经纬度,海拔高度,时间等等的信息

方向

获取方向的值调用的是

[mgr startUpdatingHeading]

magneticHeading 设备与磁北的相对角度(常用)
trueHeading 与真北的角度

区域监听(同样需要主动请求)

第一步,创建区域(CLRegion有两个子类是专门用于指定区域的:一个蓝牙范围/一个是指定圆形范围)
CLCircularRegion 指定圆形范围(常用)

第二部,开始监听

[mgr startMonitoringForRegion:(CLRegion *)];

调用代理方法
didEnterRegion/didExitRegion;(进入/离开)

地理编码

地理编码(根据位置信息,获取各种该位置的具体地理信息)
相关的对象时CLGeocoder
调用的对象方法是 geocoderAddressstring
反地理编码:根据给定的经纬度,获得具体的位置信息
调用的对象方法是reverseGeocodeLocation,传参是一个CLLocation对象。

第三方框架

推荐一个用于定位的第三方框架
INTULocationManager:https://github.com/intuit/LocationManager

0 0
原创粉丝点击