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.
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With