【发布时间】:2016-07-03 05:26:35
【问题描述】:
我在 Xcode 上使用 Objective C 编程,当我的应用程序进入后台时似乎遇到了问题。我有一个 GPS 跟踪应用程序,当用户运行该应用程序时它工作正常,但是当它进入后台时,GPS 不会一步一步地跟随用户位置。它暂停,然后当应用程序重新打开时,它会自动找到位置,但我希望 GPS 一直在运行,这样它就不会像我在附图中那样“偏离道路”1。
我已经在后台模式下开启了位置更新和后台获取功能。
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations: (NSArray *)locations
{
if (!self.timerPause) {
for (CLLocation *newLocation in locations) {
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (fabs(howRecent) < 10.0 && newLocation.horizontalAccuracy < 20)
其他代码
if (self.locationManager == nil) {
self.locationManager = [[CLLocationManager alloc] init];
}
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;
self.locationManager.distanceFilter = 10; // meters
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
【问题讨论】:
-
您的
CLLocationManager有哪些设置?特别是活动类型?您是否使用延迟位置更新?您是否将allowsBackgroundLocationUpdates设置为true?您是否正在处理didUpdateLocations中的所有位置? -
您是否将活动类型设置为
CLActivityTypeAutomotiveNavigation? -
@Paulw11 我用我的代码更新了我的帖子,请看一下。
-
为什么要丢弃超过 10 秒的位置?位置更新被推迟并不意味着它不是应该记录的有效位置
标签: ios objective-c gps