iOS:更新后核心位置关闭?
显然,为了节省电量,我们需要尽快使用 CoreLocation,并在不需要时将其关闭。我有一个跟踪用户位置的 GPS 应用程序,因此我使用几乎所有好的位置更新。我是否仍然需要以某种时间间隔关闭核心位置?
在苹果的“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];
}
Obviously to save battery we need to use CoreLocation as quickly as possible and shut it off when not needed. I have a GPS app that tracks a users location, so I use almost all good location updates. Do I still need to be turing off core location at some sort of interval?
Looking in Apple's "LocateMe" app they seem to turn it off when they find a location, but this is confusing to me, when does it get turned back on? In my case it doesn't seem to make sense to turn it off for a split second then back on.
Thoughts?
From "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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,您想要每米左右更新位置与想要节省电池并因此想要尽快使用 CoreLocation 然后将其关闭“不需要时” 的双重要求是互不相容。如果您需要每米左右更新一次位置,那么当您不需要打开位置服务时,应用程序将永远不会处于前台。根据应用程序的性质,您可能希望在进入后台或屏幕锁定时关闭位置服务...
另一种例外可能是,如果用户静止了一会儿。 Apple 提供 位置服务发生重大变化,但这似乎不适合您的需求,如果您想要逐米更新......它只是没有那个级别的分辨率。
那么可能没有一个解决方案能够同时满足您的愿望吗?
It seems to me that your dual requirements of wanting location updates every meter or so vs. wanting to save battery and so wanting to use CoreLocation as quickly as possible and then shut it off "when not needed" are mutually incompatible. If you need location updates every meter or so, there is no time when the app will be in the foreground when you will not need location services on. Depending on the nature of your app, you may well want to turn location services off when you go into the background or the screen is locked...
The one other exception might be that you'd like to turn off location services if the user is standing still for a while. Apple provide the significant change location service but that isn't going to be suitable for your needs it would seem, if you want meter-by-meter updates... it just doesn't have that level of resolution.
So there may be no solution that meets both your desires?
这取决于您需要定位的频率。如果您在应用程序运行时始终需要它,最好在 applicationWillBecomeActive 中打开位置更新并在 applicationWillResignActive 中关闭更新
it depends on how often you need location. If you need it all the time while your app is working its a better a idea to turn ON location updates in applicationWillBecomeActive and turn OFF the updates in applicationWillResignActive
您可以配置精度和距离过滤器,
这取决于您需要的精度和更新频率
You can configure the accuracy and distance filter
it depends on accuracy and update frequency you need