使用重定位框架的新手可以使用一些帮助来理解为什么我的代码也不起作用我也喜欢它。我想让它跟踪行进的距离,但是当视图启动时,它每次都会做出不同的事情。有时它会跳到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;
}
答案 0 :(得分:1)
向我跳出的第一件事是,您将startLocation
设置为nil
之后告诉位置管理员开始为您提供更新。
根据startLocation
的定义/初始化/重用方式,它可能不包含您期望的初始值。