• CoreLocation导航Demo


    CoreLocation实现定位和导航功能还是非常简单的,基本思路是:

    1.导入<CoreLocation/CoreLocation.h>头文件

    2.使用该框架内的导航管理者,创建该导航管理者对象 - CLLocationManager

    3.想要实现导航定位功能,必须先让控制器成为代理,实现其关于导航定位的代理方法即可

    4.开始定位

    想要开始定位或者导航,上面4个步骤缺一不可,具体实现Demo如下:

    //
    //  ViewController.m
    //  导航
    //
    //  Created by sleeping sun on 16/7/5.
    //  Copyright © 2016年 sleeping sun. All rights reserved.
    //
    
    #import "ViewController.h"
    #import <CoreLocation/CoreLocation.h>
    
    @interface ViewController ()<CLLocationManagerDelegate>
    @property (nonatomic,strong) CLLocationManager *manager;
    @property (nonatomic,strong) CLLocation *previousLocation;
    @property (nonatomic,assign) NSTimeInterval sumTime;
    @property (nonatomic,assign) CLLocationDistance sumDistance;
    @property (nonatomic,assign) CGFloat averSpeed;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // 1.创建定位管理者
        CLLocationManager *manager = [[CLLocationManager alloc] init];
        
        // 2.设置代理
        manager.delegate = self;
        
        // 3.判断设备
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
            NSLog(@"ios8之后版本");
            // 向用户发送请求
            /*
              在Info.plist文件中添加如下配置:
             (1)NSLocationAlwaysUsageDescription
             (2)NSLocationWhenInUseUsageDescription
             */
            [manager requestAlwaysAuthorization];
        }
        
        // 4.定位精度 - 定位精度越高,耗电量越大
        /*
         extern const CLLocationAccuracy kCLLocationAccuracyBest;
         extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
         extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
         extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
         extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
         */
        manager.desiredAccuracy = kCLLocationAccuracyBest;
        
        // 5.定位频率 - 控制定位频率,每X米定位一次
        manager.distanceFilter = 1;
        
        // 6.开始定位
        [manager startUpdatingLocation];
        
        // 7.持有定位管理者
        self.manager = manager;
    }
    
    #pragma mark - 代理方法
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
    {
        // 1.计算两个点之间的距离
        // 1.1.获取最后一个点的位置
        CLLocation *newLocation = [locations lastObject];
        
        // 1.2.判断如果第一个点有值,执行以下操作
        if (self.previousLocation) {
            // 计算两次的距离(米)
            CLLocationDistance distance = [newLocation distanceFromLocation:self.previousLocation];
            
            // 计算两次之间的时间
            NSTimeInterval dTime = [newLocation.timestamp timeIntervalSinceDate:self.previousLocation.timestamp];
            
            // 计算速度(M/S)
            CGFloat speed = distance / dTime;
            
            // 2.计算总时间 - 路程 - 平均速度
            // 2.1.总时间
            self.sumTime += dTime;
            
            // 2.2.总路程
            self.sumDistance += distance;
            
            // 2.3.平均速度
            self.averSpeed = self.sumDistance / self.sumTime;
            
            NSLog(@"行驶距离为:%f米,时间:%f秒,速度:%f米/秒",distance,dTime,speed);
            NSLog(@"行驶总路程为:%f米,总时间:%f秒,平均速度:%f米/秒",self.sumDistance,self.sumTime,self.averSpeed);
        }
        self.previousLocation = newLocation;
    }
    @end
    

      注意点:在iOS8开始,苹果对用户的隐私非常重视,所以在ios8开始,想要获得用户的授权,必须手动给用户发送授权请求:

    1.无论该应用处于前台后台,都发送位置请求

    - (void)requestAlwaysAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0) __TVOS_PROHIBITED;
    

          Requests permission to use location services whenever the app is running.

          需要在Info.plist文件当中进行配置,配置信息如下:NSLocationAlwaysUsageDescription,string字符串可以随意给。

    2.当应用处于前台,发送位置请求  

    - (void)requestWhenInUseAuthorization __OSX_AVAILABLE_STARTING(__MAC_NA, __IPHONE_8_0);
    

      Requests permission to use location services while the app is in the foreground.

          需要在Info.plist文件当中进行配置,配置信息如下:NSLocationWhenInUseUsageDescription,string字符串可以随意给。

  • 相关阅读:
    Allegro PCB Design GXL (legacy) 使用slide无法将走线推挤到焊盘的原因
    OrCAD Capture CIS 16.6 导出BOM
    Altium Designer (17.0) 打印输出指定的层
    Allegro PCB Design GXL (legacy) 将指定的层导出为DXF
    Allegro PCB Design GXL (legacy) 设置十字大光标
    Allegro PCB Design GXL (legacy) 手动更改元器件引脚的网络
    magento产品导入时需要注意的事项
    magento url rewrite
    验证台湾同胞身份证信息
    IE8对css文件的限制
  • 原文地址:https://www.cnblogs.com/sleepingSun/p/5643078.html
Copyright © 2020-2023  润新知