CLLocation speed

9

我正在开发GPS应用程序。

你知道如何检测移动设备的速度吗?

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

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

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

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

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

有什么建议吗?

提前感谢。

2个回答

27
下面的代码可以从委托方法中运行。或者,如果你想轮询,那么保留之前的位置,检查上一次轮询时位置变化的距离,并使用手动方法(如下所示)计算速度。
速度以m/s为单位计算/提供,因此乘以3.6可以得到kmph,乘以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;

    }   
}

6
兴趣所在,这两个选项中哪个更准确? - user843337
1
getDistanceFrom已经被弃用。请使用CLLocationDistance distanceChange = [newLocation distanceFromLocation:oldLocation];替换CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation]; - Wes

0

你在问题中提出的委托方法是唯一可用的。

即使每2秒访问[CLLocationManager location],你也只会在上述委托方法中收到最后接收到的坐标。

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

希望对你有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接