如何使用核心位置计算速度?

时间:2010-07-23 08:17:38

标签: iphone performance cllocation

我需要获得我的设备的速度(米每秒),这是我的代码速度总是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];

感谢

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:-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];
相关问题