我想请求关于停止CLLocationManager -startUpdatingLocation的建议。目前我正在考虑两种方法,但我不确定使用哪种方法,并且想知道其他人如何做到这一点:
Method_001:
[locationManager startUpdatingLocation];
[self performSelector:@selector(stopUpdatingLocation:) withObject:@"TimedOut" afterDelay:30];
Method_002:
[locationManager startUpdatingLocation];
然后在里面:-locationManager:didUpdateToLocation:fromLocation:
添加:
static int timeOut = 0;
timeOut++;
// Other code that checks for and stops
// when a suitable result, accuracy, age etc is found.
if(timeOut >= 4) {
[[self locationManager] stopUpdatingLocation];
timeOut = 0;
return;
}
好奇吗?
答案 0 :(得分:3)
不确定您要做什么,但我认为CLLocationManager在内部处理这些情况。只需配置它:
locManager.desiredAccuracy = 2000.0f; // 2 kilometers - hope for accuracy within 2 km.
locManager.distanceFilter = 1000.0f; // one kilometer - move this far to get another update
然后在回调didUpdateToLocation:fromLocation: 如果你有正面信号,
[locManager stopUpdatingLocation]; // stop GPS
编辑:添加signbit
if (signbit(newLocation.horizontalAccuracy)) {
// Negative accuracy means an invalid or unavailable measurement, so punt.
} else {
// this is a usable measurement.
}
答案 1 :(得分:2)
didUdpateToLocation:
方法的频率。我认为超时更可靠。
答案 2 :(得分:1)
为什么不结合两种方法并给出第三种方法(我最好的结果在一段时间内没有改善)
我写了一篇关于这个的GIT回购,你可以自由使用 https://github.com/xelvenone/M6GPSLocationManager
代码
- (void)scopeToCurrentLocationWithAcceptableAccuracy:(CLLocationAccuracy)acceptableAccuracy
maximumWaitTimeForBetterResult:(NSTimeInterval)maximumWaitTimeForBetterResult
maximumAttempts:(NSInteger)maximumAttempts
onCompletion:(M6GPSLocationManagerCompletion)completion;