首页>Program>source

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

- (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];
}
最新回答
  • 1月前
    1 #

    声明变量是您的* .m类

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

    在你的飞天 或 init 方法将它们设置为初始值

    viewDidLoad
    

    更改 _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);
            }
        }
    }
    

  • 1月前
    2 #

    记住与时间在一起的第一个位置.然后计算

    _startDate = nil;
    _distance = 0;
    
    CLLocationDistance dist = [location distanceFromLocation:initialLocation]; NSTimeInterval time = [location.timestamp timeIntervalSinceDate:initialDate]; double averageSpeed = dist/time; // If you want it in miles per hour: // double averageSpeed = dist/time * 2.236936284;

  • C ++通过const引用传递
  • javascript:阻止requestAnimationFrame一直运行