我有一个应用程序,仅使用 iPad 中的 GPS 芯片收集纬度/经度坐标(即 ios8 上没有 WiFi 或蜂窝网络)。除非应用程序闲置大约一个小时(应用程序仍然打开,但 iPad 锁定),否则它似乎可以持续工作。这是我的代码
按下 UIButton 后,我会在 IBAction 中调用 [self.locman startUpdatingLocation];
,该 UIButton 会获取坐标并将其存储到文档目录中的文本文件中。
// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
// If it's a relatively recent event, turn off updates to save power.
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 1) {
self.lat = [NSString stringWithFormat:@"%.8f",location.coordinate.latitude];
self.longString = [NSString stringWithFormat:@"%.8f",location.coordinate.longitude];
[self.locman stopUpdatingLocation];
}
}
此外,如果在一小时的中断后没有获得更新,如果用户在一小时内再次按下按钮,应用程序将随后获得更新。所以这似乎是一个热身问题。
澄清一下。这似乎是一个时间问题。出于测试目的,我会收集坐标......开车 5 分钟,然后收集坐标。我可以做很多次这样的事情,两次停留之间有大约 5 分钟的间隔。无论屏幕变成空白还是我按下“ sleep ”按钮,这都会完美地工作。当我将 ipad 单独放置大约一个小时(远远超过 5 分钟)时,问题就会出现。然后应用程序会给我坐标 0.000000 和 0.0000000。
这是我的位置管理器的实例化。
self.locman = [[CLLocationManager alloc] init];
//self.locman.pausesLocationUpdatesAutomatically = NO;
self.locman.delegate = self;
self.locman.desiredAccuracy = kCLLocationAccuracyBest;
self.locman.distanceFilter = kCLDistanceFilterNone;
self.locman.desiredAccuracy = kCLLocationAccuracyHundredMeters;
最佳答案
您不应该停止更新最近更新的位置,让框架处理频率。最好检查时间戳以确保您没有旧的位置数据 - 位置管理器通常会立即提供以前缓存的位置数据,然后在可用时提供更多最新的位置数据。
这是哪个 iOS 版本?在 iOS 8 中,您需要调用 requestAlwaysAuthorization
才能在后台接收位置更新。
您能否提供有关如何设置位置管理器的更多详细信息?
您的应用程序可能会在一段时间后被系统终止。如果存在内存泄漏,则可能会发生这种情况。
关于ios - CoreLocation 闲置后停止获取更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26114321/