【问题标题】:iOS GPS Location TrackingiOS GPS 位置追踪
【发布时间】: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 秒的位置?位置更新被推迟并不意味着它不是应该记录的有效位置
  • 参考链接:BackgroundLocationUpdates

标签: ios objective-c gps


【解决方案1】:
locationManager.allowsBackgroundLocationUpdates = YES;

在您设置了位置管理器的位置添加此项。它应该工作:)

【讨论】:

  • 干得不错!谢谢!
猜你喜欢
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2011-05-22
  • 1970-01-01
  • 2016-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多