繁体   English   中英

iOS:更新后关闭核心位置?

[英]iOS: Core location turn off after update?

显然,为了节省电池,我们需要尽快使用CoreLocation,并在不需要时将其关闭。 我有一个GPS应用程序可跟踪用户的位置,因此我几乎使用了所有良好的位置更新。 我是否仍需要每隔一定时间间隔访问一次核心位置?

在Apple的“ LocateMe”应用程序中查找时,他们似乎在找到位置时将其关闭,但这使我感到困惑,何时将其重新打开? 就我而言,将其关闭一秒钟然后重新打开似乎没有任何意义。

有什么想法吗?

来自“ LocateMe”:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    // store all of the measurements, just so we can see what kind of data we might receive
    [locationMeasurements addObject:newLocation];
    // test the age of the location measurement to determine if the measurement is cached
    // in most cases you will not want to rely on cached measurements
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;
    // test that the horizontal accuracy does not indicate an invalid measurement
    if (newLocation.horizontalAccuracy < 0) return;
    // test the measurement to see if it is more accurate than the previous measurement
    if (bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy) {
        // store the location as the "best effort"
        self.bestEffortAtLocation = newLocation;
        // test the measurement to see if it meets the desired accuracy
        //
        // IMPORTANT!!! kCLLocationAccuracyBest should not be used for comparison with location coordinate or altitidue 
        // accuracy because it is a negative value. Instead, compare against some predetermined "real" measure of 
        // acceptable accuracy, or depend on the timeout to stop updating. This sample depends on the timeout.
        //
        if (newLocation.horizontalAccuracy <= locationManager.desiredAccuracy) {
            // we have a measurement that meets our requirements, so we can stop updating the location
            // 
            // IMPORTANT!!! Minimize power usage by stopping the location manager as soon as possible.
            //
            [self stopUpdatingLocation:NSLocalizedString(@"Acquired Location", @"Acquired Location")];
            // we can also cancel our previous performSelector:withObject:afterDelay: - it's no longer necessary
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(stopUpdatingLocation:) object:nil];
        }
    }
    // update the display with the new location data
    [self.tableView reloadData];    
}

在我看来,您的双重要求是:相互之间不兼容,即需要位置每米更新一次,而要节省电池电量,因此要尽快使用CoreLocation,然后在“不需要时将其关闭 如果您需要每米左右更新一次位置信息,则无需等待任何时间,应用就不会出现在前台。 根据您应用的性质,您可能希望在进入后台或屏幕锁定时关闭位置服务。

另一个例外是,如果用户静止不动一段时间,您想关闭位置服务。 Apple提供了重要的更改位置服务,但如果您想要逐米更新,它似乎并不适合您的需求……它只是没有这种分辨率。

因此,可能没有满足您两个愿望的解决方案吗?

您可以配置精度和距离过滤器

locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
locationManager.distanceFilter = 10;

这取决于您需要的准确性和更新频率

这取决于您需要定位的频率。 如果您在应用程序运行过程中始终需要它,那么最好在applicationWillBecomeActive中打开位置更新并在applicationWillResignActive中关闭更新

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM