ユーザーの移動距離を計算するアプリケーションを開発しています。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);
}
コンソールに次のデータが表示されています......。
精度0oldd(null)new <+28.62114850、+77.37001021> +/- 80.00m(速度-1.00 mps /コース-1.00)@ 2010-06-2219:21:59+0530距離0.000000
精度0古い<+28.62114850、+ 77.37001021> +/- 80.00m(速度-1.00 mps /コース-1.00)@ 2010-06-22 19:21:59+0530新しい<+28.61670485、+ 77.37068155> +/- 80.00 m(速度-1.00 mps /コース-1.00)@ 2010-06-2219:22:00+0530距離498.211345
精度0古い<+28.61670485、+ 77.37068155> +/- 80.00m(速度-1.00 mps /コース-1.00)@ 2010-06-22 19:22:00+0530新しい<+28.62112748、+ 77.36998540> +/- 80.00 m(速度-1.00 mps /コース-1.00)@ 2010-06-2219:23:02+0530距離994.432508