ios - 在 iOS 后台更新位置

标签 ios objective-c core-location cllocationmanager

我正在开发步行轨道类型的应用程序,但面临着当应用程序处于后台时位置将如何更新的问题。我在 map 上绘制一条路径作为位置更新和一个计时器。 那么请建议我如何处理它。 这是我的代码

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{  
    if(!newLocation) return;

    if ((oldLocation.coordinate.latitude != newLocation.coordinate.latitude) &&
        (oldLocation.coordinate.longitude != newLocation.coordinate.longitude))
    {        
      // to  draw path as new location find
        jogPoint = [[JogPoint alloc] initWithCenterCoordinate:newLocation.coordinate];

        [jogPoints addObject:jogPoint];


         if([jogPoints count] >= 5) {

            [self.mapView addOverlay:jogPoint];

            CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:oldLocation.coordinate.latitude longitude:oldLocation.coordinate.longitude];


            CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
            // returns distance in meters
          distnc+=[loc1 distanceFromLocation:loc2] ;
            distanceLabel.text=[NSString stringWithFormat:@"%lf",distnc];

       //  jogInfo.distance += ([loc1 distanceFromLocation:loc2]) * 0.000621371192;

           // jogInfo.eclapsedTime = (CFAbsoluteTimeGetCurrent() - startTime) * 1000 * 60;



        }
    }
 }

最佳答案

如果您的应用程序处于后台模式,位置也会像您的应用程序一样进行跟踪/更新。活跃所以不用担心:)

只要确保你创建的 CLLocationManager 就像

self.currentLocation = [[CLLocationManager alloc]init];
self.currentLocation.desiredAccuracy = kCLLocationAccuracyHundredMeters;
self.currentLocation.delegate = self;
[self.currentLocation startUpdatingLocation];

已编辑:

如果在此处设置 distanceFilter,那么您的方法会在特定的仪表处调用,该值在 distanceFilter 中设置,否则它会在您的设备移动时更新

这对于设置/添加 需要后台模式非常重要 YourProjectName-Info.plist 文件 值为 App Registers for location update

比如

enter image description here

关于ios - 在 iOS 后台更新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18756387/

相关文章:

ios - [__NSCF字符串计数] : unrecognized selector sent to instance 0x6a7a1a0 while taking datas from json string

iOS 8 右侧 View 不可点击

ios - 您能否通过 iOS 应用确定您的用户所在的国家/地区?

ios - 多对多关系

iphone - 需要创建日历的指导

objective-c - exc 错误访问代码 2

iOS - CLLocation Manager didUpdateLocations 在一个类中被调用但在另一个类中没有被调用?

iphone - Core Location 不断请求许可

ios - 在 post 方法中发送标题

iphone - iOS,CLLocationManager : Displaying the "reenable Location Services" Popup