4

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

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



        }
    }
 }
4

2 回答 2

4

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

只要确保你的创造CLLocationManager

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

编辑:

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

这对于在文件中设置/添加 非常重要值是Require background modeYourProjectName-Info.plistApp Registers for location update

像这样

在此处输入图像描述

于 2013-09-12T05:59:03.270 回答
0

您可以使用重大变化定位服务来接收定位事件。

此服务可显着节省电量,并提供足以满足大多数应用程序的准确性。它使用设备的蜂窝无线电来确定用户的位置并报告该位置的变化,从而使系统能够比其他方式更积极地管理电源使用情况。该服务还能够唤醒当前暂停或未运行的应用程序,以提供新的位置数据。

要使用重大变化定位服务:

创建 CLLocationManager 类的一个实例,为其分配一个委托,然后调用 startMonitoringSignificantLocationChanges 方法。当位置数据可用时,位置管理器会通知其分配的委托对象。如果位置更新已经交付,您还可以直接从 CLLocationManager 对象获取最新的位置数据,而无需等待新事件的交付。

于 2013-09-12T06:04:36.373 回答