3

I am developing a speedometer application in iPhone. I have used CLLocationManager(GPS based) to measure the speed of a vehicle. But i don't get accurate speed. I have tried all the filtering methods given in various stackoverflow discussions. But i don't get any improvement. At times, the speed reaches to a greater value(>400kmph).

I dont know what goes wrong. Please suggest me any code to get the accurate speed.

Thanks in advance,

I have followed the below mentioned stack over flow discussions

Measuring velocity via iPhone SDK

Optimizing CLLocationManager/CoreLocation to retrieve data points faster on the iPhone

CLLocation speed

Why is my CLLocation speed so inaccurate?

I have currently implemented the following code:

//locationManager declaration in ViewDidLoad

locManager=[[CLLocationManager alloc]init];
locManager.delegate =self;
[locManager startUpdatingLocation];           


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

    NSTimeInterval locationAge = abs([newLocation.timestamp timeIntervalSinceNow]);

    if (locationAge > 5.0) return;
    if (newLocation.speed < 0 || newLocation.horizontalAccuracy < 0) return;
    if ((newLocation.horizontalAccuracy < (oldLocation.horizontalAccuracy - 10.0)) || (newLocation.horizontalAccuracy < 50.0)|| (newLocation.horizontalAccuracy <= 150.0))
    {
        if(oldLocation != nil)
        {
            CLLocationDistance distanceChange = [newLocation distanceFromLocation:oldLocation];

            NSTimeInterval sinceLastUpdate = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];


            double calculatedSpeed;
            calculatedSpeed = (distanceChange / sinceLastUpdate)*3.6;   // to convert the speed into kmph.

        }       
    }

}
Community
  • 1
  • 1
Jeevan
  • 31
  • 1
  • 3
  • Please provide links to the referenced StackOverflow discussions, so no-one wastes your time with solutions you've already discarded as unacceptable. Also, please post the code you are using now. – Jeremy W. Sherman Jun 07 '11 at 21:33
  • Hi Jeremy, Now i have added the code i have used. Kindly review it. – Jeevan Jun 08 '11 at 10:33

1 Answers1

4

Even if you don't like the built-in velocity stuff, it's simple to roll your own.

Speed is distance over time, right?

If you've got two CLLocations, you can do:

CLLocationDistance distance = [aLocation distanceFromLocation:bLocation];

distance is now a float of the distance between those two points, in meters. And those two CLLocations have timestamp properties too, right? Containing NSDate objects. Well...

NSTimeInterval timediff = [aDate timeIntervalSinceDate:bDate];

timediff is the difference in seconds.

So your average speed between those points in meters per second is distance over timediff. You can now calculate instantaneous speed as accurately as the frequency with which you're getting location updates.

I imagine this is all that's happening to get the speed property of the CLLocation object you're getting handed. But hey, you want to be closer to the math, go right ahead.

From there, you can convert to whatever units you like. There are 1609.344 meters in a mile. There are 3600 seconds in an hour.

NOW: This is as flawed as any pure GPS-based speed calculation, in that you're not going to get perfectly spaced tick-marks of location updates, and sometimes they're going to be way wrong. If you lose GPS signal and the device falls back to cell tower triangulation, it might put you WAY off the road somewhere, and it's going to look like you strapped on a rocket pack to get way over there that fast.

Dan Ray
  • 21,623
  • 6
  • 63
  • 87
  • Hi Dan,As you mentioned i have already tried with my own code for calculating the speed.My problem is i get the speed which is approximate,but there is a great deflection in speed even the vehicle is stationary.Is there any filtering techniques that can be used to avoid the deflection when the vehicle is stationary.. – Jeevan Jun 08 '11 at 10:37
  • Well, yeah, I mean you could sanity-check your CLLocations on the way in. You could discard locations that are suddenly a lower accuracy than you've been seeing. You could throw out locations that result in impossible speeds or impossible delta-v (I mean, it's probably not real likely you'd suddenly go from 40 to 400 mph, is it?). – Dan Ray Jun 08 '11 at 12:20