使用 corelocation 框架的新手可以使用一些帮助来理解为什么我的代码也不能按照我想要的方式工作。我希望它跟踪行进的距离,但是当视图启动时,它每次都会做不同的事情。有时它会跳到 1000 或另一个大值,或者它会随机增加。谁能明白为什么这段代码对我不起作用?
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
locationManager.distanceFilter = 100; //update every 100 meters
[locationManager startUpdatingLocation];
startLocation = nil;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (startLocation == nil)
{
totalDistanceBetween = 0;
self.startLocation = newLocation;
}
CLLocationDistance distanceBetween = [newLocation distanceFromLocation:startLocation ];
startLocation = newLocation;
totalDistanceBetween += distanceBetween;
totalDistanceBetween = totalDistanceBetween * 0.001; //convert to m value to km
NSString *tripString = [[NSString alloc]
initWithFormat:@"%.02f",
totalDistanceBetween];
distance.text = tripString;
}