基於另一個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