我已经有一些东西可以显示我当前的速度和最高速度(下面代码中的最大速度)现在我想做一些东西来计算我的平均速度和核心位置。如何?谢谢。
- (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;
}
In your init
or viewDidLoad
方法将它们设置为初始值
_distance = 0;
_lastLocation = nil;
_startDate = nil;
Change locationUpdate:
to
- (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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)