[英]Inaccurate distance travelled values
New to using the corelocation framework could use some help understanding why my code isnt working how I'd like it too. 使用corelocation框架的新手可以帮助理解为什么我的代码无法正常工作。 I want it to track the distance travelled but when the view fires up it does different things everytime.
我希望它跟踪行进的距离,但是当视图启动时,它每次都会执行不同的操作。 Sometimes itll jump to 1000 or another large value or itll increase by random increments.
有时,它会跳到1000或另一个较大的值,或者会以随机增量增加。 Can anyone see why this code isnt working for me?
谁能看到为什么此代码对我不起作用?
- (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;
}
The first thing that jumps out to me is that you set startLocation
to nil
after you tell the location manager to start giving you updates. 让我
startLocation
的第一件事是,您告诉位置管理器开始提供更新后 ,将startLocation
设置为nil
。
Depending on how startLocation
is defined/initialized/reused, it might not contain the initial value you expect it to. 根据
startLocation
的定义/初始化/重用方式,它可能不包含您期望的初始值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.