美文网首页
定位学习

定位学习

作者: 英雄出少年 | 来源:发表于2019-04-15 23:53 被阅读0次

iOS8.0之前的定位

前台定位

  1. 导入CoreLocation框架和对应的主头文件
    #import <CoreLocation/CoreLocation.h>

  2. 创建CLLcationManager对象,并设置代理
    _locationM = [[CLLocationManager alloc] init]
    _locationM.delegate = self;

  3. 调用CLLcationManager对象的startUpdatingLocation方法进行更新用户位置
    [_locationM startUpdatingLocation];

  4. 实现代理方法,接收位置参数
    -(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations

后台定位

  1. 在前台定位基础上,勾选后台模式update locations(如下图)


    DraggedImage-2.png

iOS8.0+ 至 iOS9.0之前的定位

前台定位

  1. 导入CoreLocation框架和对应的主头文件
    ''#import <CoreLocation/CoreLocation.h>

  2. 创建CLLcationManager对象,并设置代理 * 请求前台定位授权,并配置KEY *
    ''_locationM = [[CLLocationManager alloc] init];
    ''_locationM.delegate = self;
    ''if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
    ''{
    ''[_locationM requestWhenInUseAuthorization];
    ''}


    DraggedImage-1.png
  3. 调用CLLcationManager对象的startUpdatingLocation方法进行更新用户位置
    ''[_locationM startUpdatingLocation];

  4. 实现代理方法,接收位置参数
    ''-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations

后台定位

  • 方案一:在APP处于前台定位授权场景下,勾选后台运行模式update locations (如下图)


    DraggedImage-2.png

    ** 注意:如果APP处于后台,则会出现蓝条 **

  • 方案二:请求前后台定位授权,并配置KEY
    ''[_locationM requestAlwaysAuthorization];


    DraggedImage-3.png

各种授权状态讲解

''switch (status) {
''             // 用户还未决定
''         case kCLAuthorizationStatusNotDetermined:
''         {
''             NSLog(@"用户还未决定");
''             break;
''         }
''             // 问受限
''         case kCLAuthorizationStatusRestricted:
''         {
''             NSLog(@"访问受限");
''             break;
''         }
''             // 定位关闭时和对此APP授权为never时调用
''         case kCLAuthorizationStatusDenied:
''         {
''             // 定位是否可用(是否支持定位或者定位是否开启)
''             if([CLLocationManager locationServicesEnabled])
''             {
''                 NSLog(@"定位开启,但被拒");
''             }else
''             {
''                 NSLog(@"定位关闭,不可用");
''             }
''             break;
''         }
''             // 获取前后台定位授权
''         case kCLAuthorizationStatusAuthorizedAlways:
'' //        case kCLAuthorizationStatusAuthorized: // 失效,不建议使用
''         {
''             NSLog(@"获取前后台定位授权");
''             break;
''         }
''             // 获得前台定位授权
''         case kCLAuthorizationStatusAuthorizedWhenInUse:
''         {
''             NSLog(@"获得前台定位授权");
''             break;
''         }
''         default:
''             break;
''     }

iOS9.0 定位补充

前台定位

  1. 导入CoreLocation框架和对应的主头文件
    ''#import <CoreLocation/CoreLocation.h>

  2. 创建CLLcationManager对象,并设置代理 * 请求前台定位授权,并配置KEY *
    ''_locationM = [[CLLocationManager alloc] init];
    ''_locationM.delegate = self;
    ''if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
    ''{
    ''[_locationM requestWhenInUseAuthorization];
    ''}


    DraggedImage-4.png
  3. 调用CLLcationManager对象的startUpdatingLocation方法进行更新用户位置
    ''[_locationM startUpdatingLocation];

  4. 实现代理方法,接收位置参数
    ''-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations

后台定位

  • 方案一:在APP处于前台定位授权场景下,勾选后台运行模式update locations (如下图) 并且,调用以下方法,设置允许后台定位
    '' if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
    '' _locationM.allowsBackgroundLocationUpdates = YES;
    '' }


    DraggedImage-5.png

    ** 注意:如果APP处于后台,则会出现蓝条 **

  • 方案二:请求前后台定位授权,并配置KEY
    ''[_locationM requestAlwaysAuthorization];


    DraggedImage-6.png

补充

''// 作用:按照定位精确度从低到高进行排序,逐个进行定位。如果获取到的位置不是精确度最高的那个,也会在定位超时后,通过代理告诉外界
''// 注意:一个要实现代理的定位失败方法; 二:不能与startUpdatingLocation同时使用
'' [_locationM requestLocation];

CLLocation对象详解

'' //coordinate (当前位置所在的经纬度)
'' //altitude (海拔)
'' //speed (当前速度)
'' //-distanceFromLocation: (获取两个位置之间的直线物理距离)

指南针

  1. 导入CoreLocation框架和对应的主头文件
    ''#import <CoreLocation/CoreLocation.h>
  2. 创建CLLcationManager对象,并设置代理
    ''_locationM = [[CLLocationManager alloc] init];
    ''_locationM.delegate = self;
  3. 调用CLLcationManager对象的startUpdatingHeading方法进行更新设备朝向
    ''[_locationM startUpdatingHeading];
  4. 实现代理方法,获取方向参数,根据方向参数旋转图片
    ''-(void)locationManager:(nonnull CLLocationManager *)manager didUpdateHeading:(nonnull CLHeading *)newHeading
    '' {
    '' // 获取当前设备朝向(磁北方向)
    '' CGFloat angle = newHeading.magneticHeading;
    ''
    '' // 转换成为弧度
    '' CGFloat radian = angle / 180.0 * M_PI;
    ''
    '' // 反向旋转指南针
    '' [UIView animateWithDuration:0.5 animations:^{
    '' self.compassView.transform = CGAffineTransformMakeRotation(-radian);
    '' }];
    '' }
    ** 注意: 获取用户的设备朝向,不需要用户进行定位授权 **

区域监听

  1. 导入CoreLocation框架和对应的主头文件
    ''#import <CoreLocation/CoreLocation.h>

  2. 创建CLLcationManager对象,并设置代理,请求授权(iOS8.0之后才需要) * 请求前后台定位授权,并配置KEY *
    ''_locationM = [[CLLocationManager alloc] init];
    ''_locationM.delegate = self;
    ''if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0)
    ''{
    ''[_locationM requestAlwaysAuthorization];
    ''}


    DraggedImage-7.png
  3. 调用CLLcationManager对象的startMonitoringForRegion:方法进行监听指定区域
    '' // 创建区域中心
    '' CLLocationCoordinate2D center = CLLocationCoordinate2DMake(29.12345, 131.23456);
    '' // 创建区域(指定区域中心,和区域半径)
    '' CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:1000 identifier:@"小码哥"];
    '' // 开始监听指定区域
    '' [self.locationM startMonitoringForRegion:region];

  4. 实现代理方法,获取区域进入或者离开状态
    '' // 进去监听区域后调用(调用一次)
    '' -(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. 导入CoreLocation框架和对应的主头文件
    ''#import <CoreLocation/CoreLocation.h>
  2. 创建CLGeocoder对象
    ''_geoC = [[CLGeocoder alloc] init];
  3. 地理编码
  • 方案1:
    ''// 地理编码方案一:直接根据地址进行地理编码(返回结果可能有多个,因为一个地点有重名)
    '' [self.geoC geocodeAddressString:@"盛达商务园" completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {
    '' // 包含区,街道等信息的地标对象
    '' CLPlacemark *placemark = [placemarks firstObject];
    '' // 城市名称
    '' // NSString *city = placemark.locality;
    '' // 街道名称
    '' // NSString *street = placemark.thoroughfare;
    '' // 全称
    '' NSString *name = placemark.name;
    '' self.addressDetailTV.text = [NSString stringWithFormat:@"%@", name];
    '' self.latitudeTF.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.latitude];
    '' self.longtitudeTF.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.longitude];
    '' }];
  • 方案2:
    '' // 地理编码方案二:根据地址和指定区域两个条件进行地理编码(更加精确)
    '' [self.geoC geocodeAddressString:@"广州" inRegion:nil completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {
    '' // 包含区,街道等信息的地标对象
    '' CLPlacemark *placemark = [placemarks firstObject];
    '' self.addressDetailTV.text = placemark.description;
    '' self.latitudeTF.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.latitude];
    '' self.longtitudeTF.text = [NSString stringWithFormat:@"%f", placemark.location.coordinate.longitude];
    '' }];
  1. 反地理编码
    '' // 创建CLLocation对象
    '' CLLocation *location = [[CLLocation alloc] initWithLatitude:23.132931 longitude:113.375924;
    '' // 根据CLLocation对象进行反地理编码
    '' [self.geoC reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * __nullable placemarks, NSError * __nullable error) {
    '' // 包含区,街道等信息的地标对象
    '' CLPlacemark *placemark = [placemarks firstObject];
    '' // 城市名称
    '' // NSString *city = placemark.locality;
    '' // 街道名称
    '' // NSString *street = placemark.thoroughfare;
    '' // 全称
    '' NSString *name = placemark.name;
    '' self.addressDetailTV.text = [NSString stringWithFormat:@"%@", name];
    '' }];

定位的第三方框架

  1. 下载框架(locationManager框架)
    locationManager框架 下载地址

  2. 导入框架(直接拖入项目)

  3. 框架功能说明

    • 可以使用block进行获取用户位置
    • 可以设置超时时长
    • 可以取消和强制终止定位
  4. 按照github中,该框架的readme参照使用

相关文章

  • 第二周day01

    今天学到了什么? 1.学习定位position的相对定位01.png 2.学习定位的绝对定位 position:...

  • 定位学习

    iOS8.0之前的定位 前台定位 导入CoreLocation框架和对应的主头文件#import

  • iOS开发学习路线(还待完善)

    iOS定位 iOS定位简介:这里的定位,仅仅代表我个人意见,仅符合本笔记如何学习从哪里开始学习,怎么去学习来说.尚...

  • 关于定位总结感悟

    通过这几天定位书籍的学习,以及猫老师关于定位分享课的学习,对于要定位自己的重要性有了更深的理解。 01 我的定位在...

  • position:fixed会一直依据窗口进行定位吗

    在刚开始学习CSS的时候,我们都知道固定定位: 固定定位(position:fixed;)其实是绝对定位定位的子类...

  • 《定位》学习概要

    《定位》一书剖析了“满足需求”无法赢得顾客的原因,给出如何进入顾客心智以赢得选择的定位之道,做个脑图记录一下。

  • 产品定位学习

    产品定位由产品定义和用户需求两部分构成。产品定义包括使用人群、主要功能和产品特点;用户需求包括目标用户、使用场景及...

  • float定位学习

    CSS的float属性,即浮动属性,也是常用的用来定位的属性,可以使一个元素脱离正常的文档流,然后被安放到它所在容...

  • 学习定位

    今天还跟同事聊起,当年我找工作的时候遇到的一个工程老板,他希望找一个会做账,会PS,会CAD的小会计,当时我对此很...

  • 《定位》学习感受

    通过学习《定位》有两点很受用。 一、定位就是占位。 二、没有大一统的标准答案,不同的观点给予不同的灵感。

网友评论

      本文标题:定位学习

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