I am developing an application which computes the distance travelled by the user.I am using CLLocationManager class to do so.But i am getting the cached data initially and also the distance variable is increasing at a sudden rate.please help me out ...i have used the follwoing code....
Note:distance is a static var. here
- (void)viewDidLoad {
[super viewDidLoad];
//bestEffortAtLocation = nil;
oldLocat = [[CLLocation alloc]init];
newLocat = [[CLLocation alloc]init];
locationManager =[[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
// test that the horizontal accuracy does not indicate an invalid measurement
if (newLocation.horizontalAccuracy < 0) return;
NSLog(@"accuracy %d",newLocation.horizontalAccuracy);
// test the age of the location measurement to determine if the measurement is cached
// in most cases you will not want to rely on cached measurements
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
//NSLog(@"time %d",locationAge);
if (locationAge > 5.0) return;
self.oldLocat = oldLocation;
self.newLocat = newLocation;
double latDegrees = newLocation.coordinate.latitude;
NSString *lat = [NSString stringWithFormat:@"%1.5f°",latDegrees];
latLabel.text = lat;
double longDegrees = newLocation.coordinate.longitude;
NSString *longt = [NSString stringWithFormat:@"%1.5f°",longDegrees];
longLabel.text = longt;
[self computeDistanceFrom:oldLocat tO:newLocat];
}
-(void)computeDistanceFrom:(CLLocation *)oldL tO:(CLLocation *)newL
{
NSLog(@"oldd %@",oldL);
NSLog(@"new %@",newL);
distance = distance + [oldL getDistanceFrom:newL];
NSLog(@"distance %f",distance);
}
The console is displaying the following data.......
accuracy 0 oldd (null) new <+28.62114850, +77.37001021> +/- 80.00m (speed -1.00 mps / course -1.00) @ 2010-06-22 19:21:59 +0530 distance 0.000000
accuracy 0 oldd <+28.62114850, +77.37001021> +/- 80.00m (speed -1.00 mps / course -1.00) @ 2010-06-22 19:21:59 +0530 new <+28.61670485, +77.37068155> +/- 80.00m (speed -1.00 mps / course -1.00) @ 2010-06-22 19:22:00 +0530 distance 498.211345
accuracy 0 oldd <+28.61670485, +77.37068155> +/- 80.00m (speed -1.00 mps / course -1.00) @ 2010-06-22 19:22:00 +0530 new <+28.62112748, +77.36998540> +/- 80.00m (speed -1.00 mps / course -1.00) @ 2010-06-22 19:23:02 +0530 distance 994.432508