CLLocation speed
Asked Answered
S

2

9

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.

Supereminent answered 8/9, 2009 at 1:52 Comment(0)
S
27

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;

    }   
}
Stubstad answered 10/10, 2009 at 16:24 Comment(2)
Out of interest which is the better of the two options (in terms of accuracy)?Maziar
getDistanceFrom was depreciated. Replace CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation]; with CLLocationDistance distanceChange = [newLocation distanceFromLocation:oldLocation];Reeve
S
0

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

Stubstad answered 2/10, 2009 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.