iOS中的定位-CoreLocation框架

背景

因为苹果在不允许使用第三方库进行定位,即使是百度地图的定位也是在苹果API的基础上进行了封装,所以学习下iOS中定位的功能还是有必要的

CoreLocation框架

  • CLLocationManager是一个非常重要的对象
//位置服务是否可用
+ (BOOL)locationServicesEnabled __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);
//方向服务是否可用
+ (BOOL)headingAvailable __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);
//每隔指定距离定位一次(米)
@property(assign, nonatomic) CLLocationDistance distanceFilter;
//定位精度,精度越高需要定位的时间越长,越耗电
@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;

CLLocationManager 定位的结果一般会通过代理CLLocationManagerDelegate中的方法返回,具体使用方法请参考头文件

获取用户权限

  • iOS8之前
    如果想要定位需要在plist文件中位置key Privacy - Location Usage Description默认只在前台定位,如果想开启后台定位需要在开启后台模式

    Snip20150825_1.png
  • iOS8
    需要在plist文件中配置NSLocationWhenInUseUsageDescription(前台定位)
    NSLocationAlwaysUsageDescription(前后台定位)

并且需要在代码中去请求相应的权限

//请求前台定位权限
- (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
//请求前后台定位权限,即使当前权限为前台定位也会生效
- (void)requestAlwaysAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
  • 注意:如果是前台定位权限,但是开始了后台模式,在后台也是可以定位的,但是屏幕的上边会有蓝条,提示用户是哪个应用在定位

  • iOS 9
    如果想要在后台定位,除了配置NSLocationAlwaysUsageDescription(前后台定位)外,还需要手动设置allowsBackgroundLocationUpdates = YES

监听角度的改变

//每隔多少度监听一次
@property(assign, nonatomic) CLLocationDegrees headingFilter __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
//设备开始监听设备角度的改变,即设备的朝向
- (void)startUpdatingHeading __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
//监听到改变后调用此代理方法
- (void)locationManager:(CLLocationManager *)manager
       didUpdateHeading:(CLHeading *)newHeading __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

区域监听

//开始监听
- (void)startMonitoringForRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_TBD,__IPHONE_5_0);
//请求监听区域
- (void)requestStateForRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);
//进入该区域会调用此代理方法
- (void)locationManager:(CLLocationManager *)manager
    didEnterRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);
//离开该区域是调用此代理方法
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_0);
//判断状态(是否在该区域内)
- (void)locationManager:(CLLocationManager *)manager
    didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_7_0);

示例代码

 // 区域监听
/**  位置管理者 */
@property (nonatomic, strong) CLLocationManager *manager;

CLLocationCoordinate2D center = {维度, 经度};
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:区域半径 identifier:对该区域的描述];
//开始监听区域状态
[self.manager startMonitoringForRegion:region];

//开始请求区域状态
[self.manager requestStateForRegion: region]

// 进入区域
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"进入区域--%@", region.identifier);
}

// 离开区域
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    NSLog(@"离开区域--%@", region.identifier);
}
//判断状态
-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    NSLog(@"%zd", state);
}


作者:奋斗的菜鸟
链接:http://www.jianshu.com/p/ea10b9852a10
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值