首页 > 其他 > 详细

CoreLocation 下的定位跟踪测速

时间:2015-07-17 22:28:20      阅读:359      评论:0      收藏:0      [点我收藏+]

#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\n当前速度%f\n平均速度%f\n",self.sumDistance,speed,averageSpeed);

                                        

    }

    

    self.preLocation=newLocation;

    

}

 

 

@end

CoreLocation 下的定位跟踪测速

原文:http://www.cnblogs.com/tangranyang/p/4655805.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!