2017-04-07 85 views
-2

我該如何得到它?如何在每5分鐘後從後臺/前臺應用中獲取位置

-(void)updateLocation { 

[self performSelector:@selector(updateLocation) withObject:nil afterDelay:300]; 

[self.locationTracker updateLocationToServer]; 
} 

但它不是每次都工作。

+0

你在做什麼updateLocation函數?只是好奇.. – iphonic

+0

@iphonic每5分鐘重複函數調用並獲取更新位置 –

+0

使用Timer或performSelector獲取GPS位置是錯誤的,1.您不需要這樣,'CLLocationManager'能夠將GPS位置作爲每個設置/過濾器,甚至在背景中設置它2.使用計時器,你必須每次啓動GPS並試圖獲取位置,這樣的缺點是你不會得到確切的位置,因爲GPS需要幾秒鐘才能正確定位。 更好地使用'CLLocationManager'。 – iphonic

回答

0

嘗試使用的NSTimer

NSInteger minutes = 5; 
[NSTimer scheduledTimerWithTimeInterval:60 * minutes 
           target:self 
           selector:@selector(updateLocation) 
           userInfo:nil 
           repeats:YES]; 
+0

它在後臺工作,直到我的應用程序被殺死? –

+0

@BhadreshSonani當應用程序遇害/終止時,您無法獲取位置。 –

0

對於後臺更新的位置,您需要打開後臺模式從項目的能力。 enter image description here

+0

我做了位置更新 –

+0

那麼你最關心的是什麼問題。你能否再解釋一下。 – ankit

+0

我想每隔5分鐘獲得一次位置間隔時間,但有時候我在tiime上獲得了perfact位置,但有些時候我沒有獲取位置。 –

0

按照NA000022的建議在Capabilities中添加背景位置更新權限,並將其添加到您的位置管理器。

self.locationManager.pausesLocationUpdatesAutomatically = NO; 

我也建議使用

locationManager.distanceFilter 
locationManager.desiredAccuracy 

而不是更新它的持續時間。由於位置更新消耗電池比任何其他服務更快。

+0

我已經做到了,locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation; locationManager.distanceFilter = kCLDistanceFilterNone; –

+0

頂線怎麼樣?你有沒有添加代碼?我近期從事過類似的工作,並且在後臺/前臺工作良好。 –

+0

我已經添加了 –