[英]How to track user location in background?
我正在尋找一個開源應用程序或庫來跟蹤后台的用戶位置。 現在我正在嘗試使用CLLocation
和后台任務,但對於我的情況,准確性還不夠。 你能解釋一下,像“移動”,“管理員”,“endmondo”等應用程序如何創建我的路線? 我應該使用Accelerometer或/和指南針在CLLocation背景點之間創建路線嗎?
一些代碼:
//location manager init
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
#pragma mark - CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if ([self isInBackground]) {
if (self.locationUpdatedInBackground) {
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler: ^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}];
self.locationUpdatedInBackground(newLocation);
[self endBackgroundTask];
}
} else {
if (self.locationUpdatedInForeground) {
self.locationUpdatedInForeground(newLocation);
}
}
}
UPD:Justed使用下一個屬性測試了我的應用程序
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeFitness;
self.locationManager.pausesLocationUpdatesAutomatically=NO;
在這種情況下,我在1.5小時旅行期間有大約10個被解雇的事件
您需要在“Info.plist”文件中添加鍵值為“location”的關鍵UIBackgroundModes
(數組)(app注冊位置更新)。 您可以在此處查看所有背景模式。
所以你的應用程序使用位置服務 那么請閱讀位置感知編程指南 。
您需要對Info.plist進行一些更改:
在后台獲取位置事件
如果您的應用需要提供位置更新,無論應用是在前台還是后台,都有多種選擇。 首選方案是使用重要位置更改服務在適當的時間喚醒您的應用以處理新事件。 但是,如果您的應用需要使用標准位置服務,則可以將您的應用聲明為需要后台位置服務。
只有在缺少這些服務會影響其運營能力的情況下,應用才能申請后台位置服務。 此外,任何請求后台位置服務的應用程序都應使用這些服務為用戶提供切實的好處。 例如,逐向導航應用程序可能是后台定位服務的候選者,因為它需要跟蹤用戶的位置並報告何時進行下一輪轉彎。
你的問題是后台處理程序。 將其刪除並在plist文件中啟用gps后台模式。 然后你應該一直獲得全功率gps。
設置屬性pausesLocationUpdatesAutomatically=NO
這是ios6中的新功能。
允許位置管理器暫停更新可以在不犧牲位置數據的情況下延長目標設備的電池壽命。 當此屬性設置為YES時,位置管理器會在位置數據不太可能更改時暫停更新(並關閉相應的硬件)。 例如,如果用戶在使用導航應用程序時停止食物,則位置管理器可能暫停更新一段時間。 您可以通過為activityType屬性指定值來幫助確定何時暫停位置更新。
此屬性的默認值為YES。
要進行分析,請將這些方法添加到LocationManager委托中:
- (void)locationManagerDidPauseLocationUpdates:(CLLocationManager *)manager {
NSLog(@"locMan: locationManagerDidPauseLocationUpdates");
}
- (void)locationManagerDidResumeLocationUpdates:(CLLocationManager *)manager {
NSLog(@"locMan: locationManagerDidResumeLocationUpdates");
}
您可以在VC中設置監控位置內容,如下所示
在viewDidLoad方法中,如下所示
CLLocationManager locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;(Accuracy according to your need)
[locationManager startUpdatingLocation];
比你必須覆蓋CLLocationManagerDelegate協議的兩個可選委托方法
對於iOS6 +
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{}
適用於iOS 2到6
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
在這些方法中,您將獲得更新的位置。 根據需要使用它。 每次位置更新這些方法獲取調用。
這是我的解決方案,
聲明實例變量:
CLLocationManager *locationManager;
一定要包括代表
<CLLocationManagerDelegate>
在viewDidLoad中:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move, location is updated
locationManager.desiredAccuracy = kCLLocationAccuracyBest; // get best current locaton coords
locationManager.headingFilter = 1;
[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];
實現委托方法:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
NSLog(@" Current Latitude : %@",lat);
latitudeLocation.text = lat;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"",
degrees, minutes, seconds];
NSLog(@" Current Longitude : %@",longt);
longitudeLocation.text = longt;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.