简体   繁体   English

在iOS中更新后台位置

[英]update location in background in iOS

I am working on walking track type app but facing problem that how location would upadte when app is in background . 我正在研究步行轨道类型的应用程序,但面临的问题是,当应用程序在后台时,位置将如何上升。 I am drawing a path on map as location update and a timer also . 我正在地图上绘制路径作为位置更新和计时器。 Then please suggest me how i handle it. 那么请建议我如何处理它。 here is my code 这是我的代码

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



        }
    }
 }

If your Application is in background mode also location is tracking/update as like if your app. 如果您的应用程序处于后台模式,则位置也会跟踪/更新,就像您的应用程序一样。 active so don't worry about it :) 活跃所以不要担心:)

Just Make sure your create CLLocationManager such like 只需确保你的创建CLLocationManager就好

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

EDITED : 编辑:

If here set distanceFilter then your method call at specific meter which value set in distanceFilter otherwise it update whenever your device is move 如果这里设置了distanceFilter那么您在特定仪表上的方法调用在distanceFilter设置的值,否则每当您的设备移动时它都会更新

And This is very important to set/add Require background mode in YourProjectName-Info.plist file Value is App Registers for location update YourProjectName-Info.plist文件中设置/添加 Require background mode 非常重要。值是App Registers for location update

Such like 这样的

在此输入图像描述

You can use the significant-change location service to receive location events. 您可以使用重要更改位置服务来接收位置事件。

This service offers a significant power savings and provides accuracy that is good enough for most apps. 此服务可显着节省电力,并提供足以满足大多数应用程序的准确性。 It uses the device's cellular radio to determine the user's location and report changes in that location, allowing the system to manage power usage much more aggressively than it could otherwise. 它使用设备的蜂窝无线电来确定用户的位置并报告该位置的变化,从而使系统能够比其他方式更积极地管理用电量。 This service is also capable of waking up an app that is currently suspended or not running in order to deliver new location data. 此服务还能够唤醒当前暂停或未运行的应用程序,以便提供新的位置数据。

To use the significant-change location service: 要使用重要更改位置服务:

Create an instance of the CLLocationManager class, assign a delegate to it, and call the startMonitoringSignificantLocationChanges method . 创建CLLocationManager类的实例,为其分配委托,并调用startMonitoringSignificantLocationChanges方法。 As location data becomes available, the location manager notifies its assigned delegate object. 随着位置数据变得可用,位置管理器通知其分配的委托对象。 If a location update has already been delivered, you can also get the most recent location data directly from the CLLocationManager object without waiting for a new event to be delivered. 如果已经提供了位置更新,您还可以直接从CLLocationManager对象获取最新的位置数据,而无需等待传递新事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM