gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:45:34 24 4
gpt4 key购买 nike

显然,为了节省电量,我们需要尽快使用 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 然后在“不需要时”将其关闭 是互不相容的。如果您需要每隔一米左右更新一次位置,那么当您不需要位置服务时,应用程序就不会出现在前台。根据您应用的性质,您可能希望在进入后台或屏幕锁定时关闭位置服务...

另一个异常(exception)情况是,如果用户静止不动,您可能希望关闭定位服务。苹果提供the significant change location service但这似乎不适合您的需求,如果您想要逐米更新……它只是没有那样的分辨率。

所以可能没有同时满足您的需求的解决方案?

关于iphone - iOS:更新后核心位置关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7564489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com