基于另一个SO问题,我已经实现了didUpdateLocations:
方法如下:didUpdateToLocations:永不结束?
- (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];
}
}
}
}
一切有意义此代码,但我似乎无法使其正常工作。当我通过代码,我发现
- locationAge小于5,这是很好的
- 位置horizontalAccuracy小于0,这是很好
- bestEfforts水平精度相同的位置水平准确性,所以它永远不会进入该条件
- 即使它做到了,它也不会调用
stopTrackingLocation
方法,因为horizontalAccuracy的位置永远不会是< 0(第二个if语句),但是,我的位置管理器所需的准确度为-2(最佳对于导航),所以它从来没有实际上离开这个方法。
我在做什么错,或者我应该如何改变它以正确工作?
horizontalAccuracy小于0表示该位置无效。 – progrmr
为什么你想要的准确度为'-2'?它应该是一个正值,如50或100,取决于您需要多少准确度(以米为单位)? – progrmr
这就是我的想法,但这正是我所得到的。我传入'kCLLoationAccuracyBestForNavigation'并获得-2,'kCLLocationAccuracyBest'则传递-1。我把它改成了10米,看起来不错,但我仍然不明白为什么我会为其他两个获得-1/-2? – PaReeOhNos