基于另一个 SO question,我实现了didUpdateLocations:
如下方法:
- (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// Get the latest location found
CLLocation *location = [locations lastObject];
NSTimeInterval locationAge = -[location.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
if (location.horizontalAccuracy < 0) return;
if (_bestEffort == nil || _bestEffort.horizontalAccuracy >= location.horizontalAccuracy) {
_bestEffort = location;
// Let the delegate know
if (self.delegate && [self.delegate respondsToSelector:@selector(location:didUpdateLocationTo:)]) {
[self.delegate location:self didUpdateLocationTo:location];
}
if (location.horizontalAccuracy <= _locationManager.desiredAccuracy) {
// Great, we've found our final location so stop searching!
[self stopTrackingLocation];
// Let our delegate know we've finished our work
if (self.delegate && [self.delegate respondsToSelector:@selector(location:didFinishTrackingLocationWithFinalLocation:)]) {
[self.delegate location:self didFinishTrackingLocationWithFinalLocation:location];
}
}
}
}
这段代码一切都有意义,除了我似乎无法让它正常工作。当我单步执行代码时,我发现
- 位置年龄小于5岁,不错
- 位置水平精度小于 0,这很好
- bestEffors 水平精度与位置水平精度相同,所以它永远不会进入那个条件
- 即使这样做了,它也永远不会调用该
stopTrackingLocation
方法,因为位置水平精度永远不会 < 0(第二个 if 语句)但是,我的位置管理器所需的精度是 -2(最适合导航),因此它实际上永远不会离开此方法。
我做错了什么,或者我应该如何改变它才能正常工作?