gpt4 book ai didi

ios - iOS找到平均速度

转载 作者:行者123 更新时间:2023-12-01 18:15:37 26 4
gpt4 key购买 nike

我已经有一些东西可以显示我当前的速度和最高速度(下面的代码中的最大速度),现在我想做一些计算核心位置平均速度的东西。怎么样?谢谢。

- (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类

@implementation your_class
{
CLLocationDistance _distance;
CLLocation *_lastLocation;
NSDate *_startDate;
}

在您的 initviewDidLoad方法中,将它们设置为初始值
_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 - iOS找到平均速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22274801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com