我正在编写代码以不断跟踪应用CoreLocation
的用户。由于我无法始终保持GPS状态,因此我在StartMonitoringSignificantLocationChange
中使用AppDelegate
来检测重大变化。我在location
注册了UIBackgroundMode
。
在didUpdateLocations
方法中,我检查与先前记录位置的当前距离。如果它很重要,我会应用StopMonitoringSignificantLocationChange
和StartUpdatingLocation
来获取准确的位置。同时我启动计时器(30秒)以查看用户是静止还是移动。如果用户不再移动,我会再次应用StopUpdatingLocation
和StartMonitoringSignificantLocationChange
。
我还在AppDelegate: -(void)applicationWillResignActive:(UIApplication *)application
中定义了一个后台任务,如下所示:
- (void)applicationWillResignActive:(UIApplication *)application
{
if (![[GeoSampleManager sharedManager] isLiveTracking])
{
GeoSampleManager *manager = [HTSGeoSampleManager sharedManager];
[manager.locationManager stopMonitoringSignificantLocationChanges];
self.bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}];
[manager.locationManager startMonitoringSignificantLocationChanges];
}
}
当应用程序进入前台时关闭后台任务:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if(self.bgTask != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
}
}
这是我的didUpdateLocations
方法:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation* newLocation = [locations lastObject];
if(self.isLiveTracking)
{
//Test that the horizontal accuracy does not indicate an invalid measurement
if (newLocation.horizontalAccuracy < 0 || newLocation.horizontalAccuracy > 200)
{
return;
}
if([self.lastLocation distanceFromLocation:newLocation] > 0)
{
//Create and save a GeoSample
[GeoSampleManager createSampleForLocation:newLocation onTrip:self.activeTrip];
self.lastLocation = newLocation;
}
}
else
{
if([self.lastLocation distanceFromLocation:newLocation] > 0)
{
[self.locationManager stopMonitoringSignificantLocationChanges];
self.idleChecker = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(checkIdle:) userInfo:nil repeats:YES];
[self.locationManager startUpdatingLocation];
self.isLiveTracking = YES;
}
}
}
最后这里是计时器方法:
- (void)checkIdle:(NSTimer*)timer
{
if(abs([self.lastLocation.timestamp timeIntervalSinceNow]) >= 30.0)
{
//Stopping the idle-time checking timer
[self.idleChecker invalidate];
self.idleChecker = nil;
[self.locationManager stopUpdatingLocation];
[self.locationManager startMonitoringSignificantLocationChanges];
self.isLiveTracking = NO;
}
}
然而,该应用程序暂停(有时在5-10秒后,有时在10分钟后)并停止接收位置更新。因为我正在努力解决这个问题一个多月而且我已经尝试了几乎所有可能的解决方案,我非常感谢您能以任何方式帮助我解决问题。
干杯
答案 0 :(得分:2)
extern const CLLocationAccuracy kCLLocationAccuracyBestForNavigation;
extern const CLLocationAccuracy kCLLocationAccuracyBest;
extern const CLLocationAccuracy kCLLocationAccuracyNearestTenMeters;
extern const CLLocationAccuracy kCLLocationAccuracyHundredMeters;
extern const CLLocationAccuracy kCLLocationAccuracyKilometer;
extern const CLLocationAccuracy kCLLocationAccuracyThreeKilometers;
至少这个“可能”有助于管理你的GPS电池消耗,如果你一直保持运行。