在iOS中更新后台位置

时间:2013-09-12 05:56:40

标签: ios objective-c core-location cllocationmanager

我正在研究步道式应用,但面临的问题是当应用在后台时,位置会如何提升。我正在地图上绘制路径作为位置更新和计时器。 那么请建议我如何处理它。 这是我的代码

- (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;



        }
    }
 }

2 个答案:

答案 0 :(得分:4)

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

确保您的创建CLLocationManager

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

已编辑:

如果此处设置了distanceFilter,则您的方法会在特定的电表上调用distanceFilter中设置的值,否则只要您的设备移动就会更新

这对Require background mode文件中的设置/添加 YourProjectName-Info.plist非常重要 值为App Registers for location update

如此

enter image description here

答案 1 :(得分:0)

您可以使用重要更改位置服务来接收位置事件。

  

此服务可显着节省电力并提供准确性   这对大多数应用来说已经足够了。它使用设备的蜂窝无线电   确定用户的位置并报告该位置的变化,   允许系统更加积极地管理电源使用   否则就可以了。此服务还能够唤醒应用程序   当前暂停或未运行以便提供新的   位置数据。

要使用重要更改位置服务:

  

创建CLLocationManager类的实例,分配一个委托   它,并调用startMonitoringSignificantLocationChanges方法   。随着位置数据变得可用,   位置管理器通知其分配的委托对象。如果是一个位置   更新已经发布,您也可以获得最新的   直接来自CLLocationManager对象的位置数据   等待新事件的发送。

相关问题