美文网首页
coreLocation位置定位

coreLocation位置定位

作者: 陈水寒 | 来源:发表于2017-03-21 10:52 被阅读28次

1、 iOS 7、iOS 8、iOS 9定位都不一样,如果要兼容老版本需要做不同的适配

初始化代码如下:

        CLLocationManager lM = [[CLLocationManager alloc] init];
        // 设置定位精确度
        lM.desiredAccuracy = kCLLocationAccuracyBest;
        lM.delegate = self;
        
        // ios8 适配
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            [lM requestAlwaysAuthorization];
        }
        
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
            // 需要开启后台权限
            lM.allowsBackgroundLocationUpdates = YES;
        }      

适配ios8 还需要在info.plist文件中添加以下键值:NSLocationAlwaysUsageDescription
适配ios9 还需要开启后台权限,配置如下图

开启后台权限示意图.png

2、指南针

初始化代码如下:

        CLLocationManager lM = [[CLLocationManager alloc] init];
        lM.delegate = self;
        [lM startUpdatingHeading];
// 代理方法
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
     /**
     *  CLHeading
     *  magneticHeading : 磁北角度
     *  trueHeading : 真北角度
     */
    CGFloat angle = newHeading.magneticHeading;
    // 角度转弧度
    CGFloat radian = angle / 180 * M_PI;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.compassImageView.transform = CGAffineTransformMakeRotation(radian);
    }];
}

3、区域监控

给出一个中心点和半径为监控区域,当进入或者离开该区域时,通过代理通知控制器
初始化代码及代理如下

    // 定位管理者
    CLLocationManager lM = [[CLLocationManager alloc] init];
    lM.delegate = self;

    // 给出一个中心位置
    CLLocationCoordinate2D center = {40.905206,116.390356};
    // 设置监控区域的中心位置、监控区域以及标识
    CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:1000 identifier:@"区域监听"];
    // 开启区域监听
    [lM startMonitoringForRegion:region];

    // 第一次进入区域监听的时候不会调用代理方法
    // locationManager:(CLLocationManager *)manager didEnterRegion和
    // locationManager:(CLLocationManager *)manager didExitRegion,
    // 所以需要第一次进入的时候需要判断下当前的区域状态
    [self.lM requestStateForRegion:region];
// 代理方法
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"进入了监控区域");
}

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(nonnull CLRegion *)region
{
    NSLog(@"离开了监控区域");
}

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    // 当前所在区域状态
    NSLog(@"%zd",state);
}

相关文章

  • coreLocation位置定位

    1、 iOS 7、iOS 8、iOS 9定位都不一样,如果要兼容老版本需要做不同的适配 初始化代码如下: 适配io...

  • iOS14开发-定位与地图

    定位 CoreLocation 是 iOS 中用于设备定位的框架。通过这个框架可以实现定位进而获取位置信息如经度、...

  • iOS定位的使用

    简介 定位,顾名思义,就是确定你所在的位置。 一、介绍 • 定位使用CoreLocation框架 •功能 • iO...

  • 地图sdk详解

    1 获取用户位置 需要导入CoreLocation框架 使用定位服务前,通常需要检查定位服务是否可用 : [CLL...

  • 地图的定位与导航

    1、定位 获取定位需要用到 CoreLocation/CoreLocation.h 框架 1)...

  • 关于地图定位

    CoreLocation框架 一. iOS8.0之前的定位(✨✨✨✨✨) 前台定位导入CoreLocation框架...

  • iOS中的定位功能

    iOS中的定位功能 CoreLocation框架(CoreLocation.framework)可用于定位设备当前...

  • iOS 定位&地图

    获取当前位置 Corelocation获取位置满足一下2个要求:得到用户的许可确保设备启用定位服务 使用 1 . ...

  • CoreLocation基本操作之区域检测和运动平均速度

    1.导入#import定位框架 2.声明对象,并遵守定位...

  • Swift - 使用CoreLocation实现定位(经纬度、海

    CoreLocation是iOS中一个提供设备定位的框架。通过这个框架可以实现定位处理,从而获取位置数据,比如经度...

网友评论

      本文标题:coreLocation位置定位

      本文链接:https://www.haomeiwen.com/subject/zhiwnttx.html