本文介绍了iOS找到平均速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经有一些东西可以显示我当前的速度和最高速度(下面代码中的最大速度)现在我想制作能够计算核心位置的平均速度的东西。怎么样?谢谢。
I already have something that shows me my current speed and top speed (max speed in the code below) Now I want to make something that calculates my average speed with core location. How? thanks.
- (void)locationUpdate:(CLLocation *)location {
speedLabel.text = [NSString stringWithFormat:@"%.2f", [location speed]*2.236936284];
这是最高速度的浮动
float currentSpeed = [location speed]*2.236936284;
if(currentSpeed - maxSpeed >= 0.01){
maxSpeed = currentSpeed;
maxspeedlabel.text = [NSString stringWithFormat: @"%.2f", maxSpeed];
}
推荐答案
声明变量是你的* .m类
Declare variable is your *.m class
@implementation your_class
{
CLLocationDistance _distance;
CLLocation *_lastLocation;
NSDate *_startDate;
}
在 init
或 viewDidLoad
方法将它们设置为初始值
In your init
or viewDidLoad
method set them to initial values
_distance = 0;
_lastLocation = nil;
_startDate = nil;
将 locationUpdate:
更改为
- (void)locationUpdate:(CLLocation *)location {
speedLabel.text = [NSString stringWithFormat:@"%.2f", [location speed]*2.236936284];
if (_startDate == nil) // first update!
{
_startDate = location.timestamp;
_distance = 0;
}
else
{
_distance += [location distanceFromLocation:_lastLocation];
_lastLocation = location;
NSTimeInterval travelTime = [location.timestamp timeIntervalSinceDate:_startDate];
if (travelTime > 0)
{
double avgSpeed = _distance / travelTime;
AVGspeedlabel.text = [NSString stringWithFormat: @"%.2f", avgSpeed];
NSLog(@"Average speed %.2f", avgSpeed);
}
}
}
重置平均速度
_startDate = nil;
_distance = 0;
这篇关于iOS找到平均速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!