簡體   English   中英

在iOS中更新后台位置

[英]update location in background in iOS

我正在研究步行軌道類型的應用程序,但面臨的問題是,當應用程序在后台時,位置將如何上升。 我正在地圖上繪制路徑作為位置更新和計時器。 那么請建議我如何處理它。 這是我的代碼

- (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文件中設置/添加 Require background mode 非常重要。值是App Registers for location update

這樣的

在此輸入圖像描述

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

此服務可顯着節省電力,並提供足以滿足大多數應用程序的准確性。 它使用設備的蜂窩無線電來確定用戶的位置並報告該位置的變化,從而使系統能夠比其他方式更積極地管理用電量。 此服務還能夠喚醒當前暫停或未運行的應用程序,以便提供新的位置數據。

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

創建CLLocationManager類的實例,為其分配委托,並調用startMonitoringSignificantLocationChanges方法。 隨着位置數據變得可用,位置管理器通知其分配的委托對象。 如果已經提供了位置更新,您還可以直接從CLLocationManager對象獲取最新的位置數據,而無需等待傳遞新事件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM