3

我需要获取设备的速度(米/秒),这是我的代码,速度始终为 0,我不明白。

  - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

        double gpsSpeed2 = newLocation.speed;

        labelm.text = [NSString stringWithFormat:@"%f",gpsSpeed2];
    }



locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];

谢谢

4

2 回答 2

1

我以前从未尝试过,但如果您将位置管理器中的 distanceFilter 设置为 1 米之类的数字并计算时间,可能会更好。然后计算速度。

于 2010-07-23T08:29:54.570 回答
-1

您需要自己进行计算。像这样的东西:

if (oldLocation != nil)
{
   CLLocationDistance distance = [newLocation getDistanceFrom:oldLocation];
   NSTimeInterval timeElapsed = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];

   // Use distance and timeElapsed to calculate speed
}
else {
   // We don't have and old time, so can't calculate speed
}    

oldLocation = [newLocation retain];
于 2010-07-23T08:28:20.390 回答