ios - didUpdateLocations 每秒通过延迟更新不断调用

标签 ios cllocationmanager

我正在尝试实现延迟位置更新以获得更好的电池消耗。 我正在这样启动我的位置管理器:

- (void)initCoreLocation
{
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.pausesLocationUpdatesAutomatically = YES;
    self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;

    //Très important pour iOS9 !
    if ([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
        self.locationManager.allowsBackgroundLocationUpdates=YES;
    }

    if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [self.locationManager requestAlwaysAuthorization];
    }

    [self.locationManager startUpdatingLocation];
    [self.locationManager startMonitoringSignificantLocationChanges];
}

然后像这样开始延迟更新:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{

    if (!self.deferringUpdates) {
        [self.locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:30];
        self.deferringUpdates = YES;
    }
}

-(void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error { // Stop deferring updates
    if(error) {
        NSLog(@"error");
    }
    NSLog(@"didFinishDeferredUpdates");
    self.deferringUpdates = NO;
}

我每 30 秒记录一次 didFinishDeferredUpdates,但是 didUpdateLocations 每秒都在调用,消除了任何优化电池消耗的尝试。是否假设位置管理器每 30 秒调用一次 didUpdateLocations

最佳答案

也许您没有采用正确的方法,因为 allowDeferredLocationUpdatesUntilTraveled() 告诉 GPS 硬件在内部存储新位置,直到满足指定的距离或超时条件。

来自 iOS 开发者库:

If your app is in the foreground, the location manager does not defer the deliver of events but does monitor for the specified criteria. If your app moves to the background before the criteria are met, the location manager may begin deferring the delivery of events. Link

关于ios - didUpdateLocations 每秒通过延迟更新不断调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33822590/

相关文章:

iphone - 检查 locationServicesEnabled 始终返回 YES,无论是否手动切换开关来决定是否启用位置服务

ios - Swift 中的反向地理编码位置

ios - 快速创建围绕图像的圆形进度条

ios - 使用xib,如何使高度为0.5

ios - 在 ios 中的动画期间,对 imageview 的手势不起作用

iphone - 位置设置与功耗统计

ios - Ionic3 - iOS - 滑动菜单样式问题

objective-c - 我应该删除或隐藏 MKMapView 中的注释吗?

ios - iBeacon 开始在应用程序上转换广告确实进入了后台

ios - 当我重新启动我的设备时,我的 IOS 应用程序没有获得任何区域更新有什么想法吗?