当我第一次开始位置更新时,该值不会始终为 0。有时是这样,但有时我会得到略高于 0 的值。我会得到一个值,例如 2。所以粗略时,我停止并重新开始,然后得到一个较小的值并继续这样做,直到我得到零。即使我想要的距离过滤器是 250 米,但在开始时会得到低于此值的值!为什么会发生这种情况以及我该如何处理。以下是在室内启动时尤其存在的问题(让我认为这是一些 GPS 初始建立问题)。我可以通过一个简单的警报视图来处理这个问题,以在开始之前确认用户在户外,但这有时会在户外发生,所以我希望解决这个问题。此外,当它确实从 0 开始并且我立即开始驾驶时,我得到的值不准确。
我对解决方案的猜测:在位置更新开始之前,需要确认我是否有可靠的 GPS 连接。
在 CLLocationManager 类参考中看不到对上述解决方案有任何帮助的任何内容。
这是我的意思的一个例子,当它不是从 0 开始时开始和停止。
Trigger Start - > Location = 1.1 km
Trigger Stop
Trigger Start - > Location = .98 km
Trigger Stop
Trigger Start - > Location = .55 km
Trigger Stop
Trigger Start - > Location = .02 km
Trigger Stop
Trigger Start - > Location = 0 km (finally where it should start from)
这是我的代码:
@interface DriveLog (){
CLLocationDistance totalDistanceBetween;
NSString *tripString;
Settings *settingsVC;
CGFloat km;
}
- (void)viewDidLoad{
totalDistanceBetween = 0;
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
locationManager.distanceFilter = 250;
startLocation = nil;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (IBAction)start:(id)sender {
[locationManager startUpdatingLocation];
}
- (IBAction)stop:(id)sender {
totalDistanceBetween = 0;
startLocation = nil;
[locationManager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if (startLocation == nil){
totalDistanceBetween = 0;
self.startLocation = newLocation;
}
CLLocationDistance distanceBetween = [newLocation distanceFromLocation:startLocation ];
self.startLocation = newLocation;
totalDistanceBetween += distanceBetween;
if(kmOrMile == 0){
km = totalDistanceBetween * 0.001;
}else{
km = totalDistanceBetween * 0.000621371192237334;
}
tripString = [[NSString alloc]
initWithFormat:@"%.02f%@",
km, kmOrMileString];
distance.text = tripString;
}