我正在开发一个计算用户行进距离的应用程序。我正在使用CLLocationManager类来完成这项工作,但我最初获取了缓存数据,并且距离变量突然增加。请帮助我...我已经使用了以下代码....Corelocation不正确的距离
注意:distance是一个静态var。这里
- (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(空)新< 28.62114850,77.37001021> +/-80.00米 (速度-1.00 MPS /当然-1.00)@ 2010-06-22 19时21分59秒0530距离 0.000000
精度0 oldd < 28.62114850,77.37001021> +/-80.00米(速度-1.00 MPS /课程-1.00)@ 2010-06-22 19:21:59 +0530新< 28.61670485, 77.37068155> +/-80.00米(速度-1.00 MPS /当然-1.00)@ 2010-06-22 19时22分○○秒0530 距离498.211345
精度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距离994.432508
谢谢progrmr,但你可以告诉我如何删除缓存的数据使用时间戳........,如果上述方法来计算距离是错误的,那么我应该如何着手......是任何其他方式来计算距离.....因为我已经看到许多应用程序在App Store中做同样的工作,如计算距离,速度等.pls给我你的宝贵意见。 – Siddharth 2010-06-23 06:42:52
我添加了一个代码示例以显示如何删除旧的缓存数据,但是您已经在代码中执行了该操作。 – progrmr 2010-06-23 14:36:15