CLLocation速度 [英] CLLocation speed

查看:287
本文介绍了CLLocation速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing GPS application. Do you know about how to detect speed of mobile device ?

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

Actually, I need to detect the speed every 2 seconds.

我知道当位置发生变化时,会调用 didUpdateToLocation 方法。

I know didUpdateToLocation method is called when location changed.

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

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

But I think this method is not suitable for my issue.

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

So, Do I need to check speed of [CLLocationManager location] in 2 seconds ?

有任何建议吗?

提前致谢。

推荐答案

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

How about the code below which works from the delegate method. Alternatively, if you did want to poll, then keep your previous location and check the distance changed from the last poll and use the manual method (also shown below) to calculate the speed.

速度以m / s为单位计算/提供,因此对于公里数乘以3.6或对于英里/小时乘以2.23693629。

Speed is calculated/provided in m/s so multiply by 3.6 for kmph or 2.23693629 for 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;

    }   
}

这篇关于CLLocation速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆