In my application, I am using LocationManager for getting location, also I want calculate DISTANCE, SPEED and TIME.
From Location manager I am getting time and distance successfully,But when I calculate the speed from coordinates it is showing wrong values.
For getting location I am using following code.
CLLocationManager *locManager ;
float fltDistanceTravelled;
double calculatedSpeed;
//===============
locManager=[[CLLocationManager alloc] init];
locManager.delegate=self;
locManager.distanceFilter=25.0f;
locManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
[locManager startUpdatingLocation];
//=====================
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if(newLocation && oldLocation)
{
// getting distance
fltDistanceTravelled +=[self getDistanceInKm:newLocation fromLocation:oldLocation];
}
if(oldLocation != nil)
{
CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];
NSTimeInterval sinceLastUpdate = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
// calculate speed
calculatedSpeed= (distanceChange / sinceLastUpdate) * 3.6;
}
}
but this code return wrong values of calculatedSpeed (speed) of the car.
You are already getting the speed data in the delegate method.
CLLocationhas aspeedpropertyRather than try and calculate it yourself, just get it from
newLocation.speed.