I'm still trying to figure out how to calculate the current speed of my application. From my point of view the calculation should be correct but now I only get values of 0.0 or slightly above.
I tried it with my car when I drove about 50km/h. I would be very grateful if somebody could help me.
Here is my code:
Code:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSLog(@"Updating location");
NSDate *newLocationEventDate = newLocation.timestamp;
//no pointer because it is a C-Class
NSTimeInterval howRecentNewLocation = [newLocationEventDate timeIntervalSinceNow];
//initial Update, no oldLocation available therefore it won't be checked how good the data is
if(firstUpdate) {
if (signbit(newLocation.horizontalAccuracy)) {
// Negative accuracy means an invalid or unavailable measurement
NSLog(@"LatLongUnavailable");
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Error Alert" message:@"LatLongUnavailable" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"exit", nil];
[myAlert show];
[myAlert release];
}
else{
myLocation = [newLocation retain];
[self updatedGPS:newLocation];
firstUpdate = NO;
[mySpinner stopAnimating];
isUpdating = NO;
return;
}
}
if(isUpdating){
if (signbit(newLocation.horizontalAccuracy)) {
// Negative accuracy means an invalid or unavailable measurement
NSLog(@"LatLongUnavailable");
UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Error Alert" message:@"LatLongUnavailable" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"exit", nil];
[myAlert show];
[myAlert release];
}
else{
timesFired++;
//filter cached and too old locations
//1.Version:
//if((myLocation.horizontalAccuracy > newLocation.horizontalAccuracy) && (howRecentNewLocation < -0.0 && howRecentNewLocation > -10.0)) {
//2.Version
if(howRecentNewLocation < -0.0 && howRecentNewLocation > -10.0){
//release the cached location, if available
if(myLocation)
[myLocation release];
myLocation =[newLocation retain];
}
[self updatedGPS:myLocation];
timesFired = 0;
[mySpinner stopAnimating];
return;
}
}
}
}
- (void)updatedGPS:(CLLocation *)goodLocation{
//methode variables for calculating the current and avg speed
CLLocationDistance distanceMoved;
NSTimeInterval timeElapsed;
NSLog(@"uptadedGPS entered:");
NSLog(@"Lat: %f", goodLocation.coordinate.latitude);
NSLog(@"Long: %f", goodLocation.coordinate.longitude);
//calculate distance moved and time elapsed, but only if we have an "old" location
if(oldLocation != nil) {
distanceMoved = [goodLocation getDistanceFrom:oldLocation];
timeElapsed = [goodLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
}
else{
distanceMoved = 0.0;
timeElapsed = 1.0;
}
//formatting to km/h
currentSpeed = (distanceMoved*0.001)/(timeElapsed*360);
currentSpeedLabel.text = [NSString stringWithFormat:@"%3.1f", currentSpeed];
NSLog(@"currentSpeed: %f", currentSpeed);
}
//save the latest goodLocation for calculating the speed
oldLocation = [goodLocation retain];
}