zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    数据库事务的特性以及隔离级别
    非受检异常(运行时异常)和受检异常的区别等
    在测试crontab执行脚本的时候,修改了linux的系统时间,crontab不执行了。
    perl的foreach循环的坑
    javascript中快速求数组的全部元素的相加之和
    js 百度地图
    PHP 取前一天或后一天、一个月时间
    php截取字符串函数
    js jquery 基本元素操作
    PHP 替换标签和标签内的内容
  • 原文地址:https://www.cnblogs.com/tangranyang/p/4655805.html
Copyright © 2011-2022 走看看