2009-09-08 53 views
8

我正在开发GPS应用程序。 你知道如何检测移动设备的速度吗?CLLocation速度

其实,我需要每2秒检测一次速度。

我知道didUpdateToLocation当位置改变时调用方法。

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

但我认为这种方法是不适合我的问题。

那么,我需要在2秒内检查[CLLocationManager location]的速度吗?

有什么建议吗?

在此先感谢。

回答

26

如何低于从委托方法工作的代码。 或者,如果您确实想要轮询,请保留您之前的位置,并检查上次轮询后更改的距离,并使用手动方法(也在下面显示)计算速度。

速度是以m/s计算/提供的,因此乘以3.6 km/km或2.23693629/mph。

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    //simply get the speed provided by the phone from newLocation 
    double gpsSpeed = newLocation.speed; 

    // alternative manual method 
    if(oldLocation != nil) 
    { 
     CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation]; 
     NSTimeInterval sinceLastUpdate = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp]; 
     double calculatedSpeed = distanceChange/sinceLastUpdate; 

    } 
} 
+5

从两个选项(准确性而言)哪一个更好? – 2011-10-08 18:10:13

+1

getDistanceFrom已折旧。替换CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];与CLLocationDistance distanceChange = [newLocation distanceFromLocation:oldLocation]; – Wes 2013-04-16 01:27:00

0

您只能真正使用您在问题中建议的委托方法。

即使您每2秒钟访问一次[CLLocationManager位置],您也只会收到上次在上述委托方法中收到的坐标。

为什么需要每两秒轮询一次?在某些情况下,iphone可以在更短的时间内更新它的坐标。

HTH