簡體   English   中英

如何在ios中以終止狀態獲取每200米的位置

[英]How to get location on each 200 meters in terminated state in ios

我試圖在應用程序的終止狀態下獲取用戶的位置。 我通過startMonitoringSignificantLocationChanges執行此操作,但它在 3 公里或 5 分鍾后給出位置。 所以無法正確創建路線。 請指導我如何做到這一點。

 if (_anotherLocationManager)
    [_anotherLocationManager stopMonitoringSignificantLocationChanges];

self.anotherLocationManager = [[CLLocationManager alloc]init];
_anotherLocationManager.delegate = self;
_anotherLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
_anotherLocationManager.activityType = CLActivityTypeOtherNavigation;

if(IS_OS_8_OR_LATER) {
    [_anotherLocationManager requestAlwaysAuthorization];
}
[_anotherLocationManager startMonitoringSignificantLocationChanges];

我自己解決了我的問題...

創建一個 locationManager 對象並像這樣分配它

 self.locationManager = [[CLLocationManager alloc]init]; // initializing locationManager
_locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; // setting the accuracy
[self.locationManager requestAlwaysAuthorization];

if([self.locationManager respondsToSelector:@selector(allowsBackgroundLocationUpdates)]) {
    [self.locationManager setAllowsBackgroundLocationUpdates: YES];
}

self.locationManager.distanceFilter = 20.0;

[self.locationManager startMonitoringSignificantLocationChanges];

self.locationManager.activityType=CLActivityTypeAutomotiveNavigation;

[self.locationManager startUpdatingLocation];

self.locationManager.pausesLocationUpdatesAutomatically = YES;

self.locationManager.delegate = self;

現在設置位置管理器委托方法。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
{
    if (newLocation.speed>7){
      // here you got location, i settled speed 7 for my convenience.  
    }

    if (newLocation.horizontalAccuracy <= self.locationManager.desiredAccuracy) {
        //Desired location Found
        [self.locationManager stopUpdatingLocation ];
    }
}

你必須寫這個stopUpdatingLocation ,否則你的電池消耗會增加這么高。

使用地理圍欄來達到同樣的目的。

  • 創建一個半徑為 200 米的地理圍欄。
  • 默認情況下,notifyOnExit 為 true
  • 實現 LocationManager 的委托didExitRegion
  • 對於終止狀態,應用程序將在 didFinishLaunchingWithOptions 中使用UIApplication.LaunchOptionsKey.location
  • 在位置啟動鍵上創建一個位置管理器對象的實例,您可以獲得該位置退出的區域,並在每個出口處繼續創建 200mtr 圍欄。

您不會通過重要的位置監控獲得 200 米的粒度或連續跟蹤。

您是否在文檔中看到過這些注釋:

顯着變化位置服務僅在設備位置發生顯着變化(例如 500 米或更遠)時才提供更新。

如果 GPS 級別的精度對您的應用程序並不重要,並且您不需要持續跟蹤,則可以使用重大變化位置服務。

暫無
暫無

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

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