【问题标题】:update location in background in iOS在 iOS 后台更新位置
【发布时间】:2013-09-12 05:56:39
【问题描述】:

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

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



        }
    }
 }

【问题讨论】:

    标签: ios objective-c core-location cllocationmanager


    【解决方案1】:

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

    只要确保你创建 CLLocationManager 之类的

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

    已编辑:

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

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

    比如

    【讨论】:

    • 会不会更新定时器,不需要在info.plist文件中添加所需的后台模式。
    • 如果 distanceFilter 为 kCLDistanceFilterNone,didUpdateToLocation 会自动调用您的移动。
    • 你的意思是说我不需要为它做任何额外的代码。
    • @Arpi - 是的我',正是:)
    • 确保你的距离过滤器没有设置,也没有停止stopUpdatingLocation所以..它将继续调用didUpdateToLocation 方法
    【解决方案2】:

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

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

    要使用显着变化的位置服务:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-16
      • 2018-03-11
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多