CLLocation speed

故事扮演 提交于 2019-12-28 12:05:09

问题


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

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

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.

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

Any suggestion ?

Thanks in advance.


回答1:


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.

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;

    }   
}



回答2:


You can only really use the delegate method you have suggested in your question.

Even if you access the [CLLocationManager location] every 2 seconds, you will only receive the coordinate you last received in the delegate method above.

Why the need to poll every two seconds? The iphone can update it's coordinates in less time on some cases.

HTH



来源:https://stackoverflow.com/questions/1391589/cllocation-speed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!