• CoreLocation 下的定位跟踪测速


    #import "ViewController.h"

    #import <CoreLocation/CoreLocation.h>

     

    @interface ViewController ()<CLLocationManagerDelegate>

     

     

    @property(nonatomic,strong)CLLocationManager*manager;;

    @property(nonatomic,strong)CLLocation *preLocation;

    @property(nonatomic,assign)NSTimeInterval sumtime;

    @property(nonatomic,assign)CGFloat sumDistance;

     

     

    @end

     

    @implementation ViewController

     

    -(CLLocationManager*)manager

    {

        if (_manager==nil) {

            _manager=[[CLLocationManager alloc]init];

        }

        return _manager;

    }

     

    - (void)viewDidLoad {

        [super viewDidLoad];

        self.manager.delegate=self;

        //判断ios7 或者8

        if([[UIDevice currentDevice].systemVersion doubleValue]>8.0)

        {

            //[self.manager requestWhenInUseAuthorization];

            [self.manager requestAlwaysAuthorization];

        }

        [self.manager startUpdatingLocation];

    }

     

    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

    {

        CLLocation *newLocation=[locations lastObject];

        

        if (self.preLocation==nil) {

            CLLocationDirection dierction=[newLocation distanceFromLocation:self.preLocation ];

            NSTimeInterval dTime=[newLocation.timestamp timeIntervalSinceDate:self.preLocation.timestamp];

            //计算速度

            CGFloat speed=dierction /dTime;

            self.sumDistance+=dierction;

            self.sumtime+=dTime;

            

            //平均速度

            CGFloat averageSpeed=self.sumDistance/self.sumtime;

            

            NSLog(@"行驶距离%f 当前速度%f 平均速度%f ",self.sumDistance,speed,averageSpeed);

                                            

        }

        

        self.preLocation=newLocation;

        

    }

     

     

    @end

  • 相关阅读:
    [转载]Back up all of your mysql databases nightly
    编写windows7 bat运行脚本
    Windows7开通Internet信息服务
    【LeetCode题解】7_反转整数
    【LeetCode题解】350_两个数组的交集Ⅱ
    【LeetCode题解】349_两个数组的交集
    【LeetCode题解】94_二叉树的中序遍历
    【LeetCode题解】144_二叉树的前序遍历
    【LeetCode题解】2_两数相加
    【LeetCode题解】530_二分搜索树的最小绝对值差
  • 原文地址:https://www.cnblogs.com/tangranyang/p/4655805.html
Copyright © 2020-2023  润新知