我正在开发一个计算用户行进距离的应用程序。我正在使用 CLLocationManager 类来执行此操作,但我最初获取的是缓存数据,并且距离变量也在突然增加。请帮帮我......我使用了以下代码......
注意:距离是一个静态变量。这里
- (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);
}
控制台正在显示以下数据.......
精度 0 oldd (null) new <+28.62114850, +77.37001021> +/- 80.00m(速度 -1.00 mps / course -1.00)@ 2010-06-22 19:21:59 +0530 距离 0.000000
精度 0 oldd <+28.62114850, +77.37001021> +/- 80.00m(速度 -1.00 mps / course -1.00)@ 2010-06-22 19:21:59 +0530 new <+28.61670485, +77.37068155> +/- 80.00 m(速度 -1.00 mps / 路线 -1.00)@ 2010-06-22 19:22:00 +0530 距离 498.211345
精度 0 oldd <+28.61670485, +77.37068155> +/- 80.00m(速度 -1.00 mps / course -1.00)@ 2010-06-22 19:22:00 +0530 new <+28.62112748, +77.36998540> +/- 80.00米(速度 -1.00 mps / 课程 -1.00)@ 2010-06-22 19:23:02 +0530 距离 994.432508