OGeek|极客世界-中国程序员成长平台

标题: ios - CoreLocation 在空闲后停止获取更新 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:56
标题: ios - CoreLocation 在空闲后停止获取更新

我有一个应用程序,它仅使用 iPad 中的 GPS 芯片(IE 没有 wifi 或 ios8 上的蜂窝网络)收集纬度/经度坐标。除非应用程序空闲大约一个小时,否则它似乎始终如一地工作(应用程序仍处于打开状态但 iPad 锁定。)这是我的代码

我在 IBAction 中调用 [self.locman startUpdatingLocation]; 在按下 UIButton 时获取坐标并将它们存储到文档目录中的文本文件中。

// Delegate method from the CLLocationManagerDelegate protocol.
- (void)locationManagerCLLocationManager *)manager
     didUpdateLocationsNSArray *)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;



Best Answer-推荐答案


您不应该停止更新最近更新的位置,让框架来处理频率。最好检查时间戳以确保您没有旧的位置数据 - 位置管理器通常会立即提供以前缓存的位置数据,然后在可用时提供更多最新的位置数据。

这是哪个 iOS 版本?在 iOS 8 中,您需要调用 requestAlwaysAuthorization 以便在后台接收位置更新。

您能否详细说明您是如何设置位置管理器的?

您的应用可能会在一段时间后被系统杀死。如果存在内存泄漏,可能会发生这种情况。

关于ios - CoreLocation 在空闲后停止获取更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26114321/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4