我正在寻找一个开源的应用程序或库来在后台跟踪用户位置。现在我正在尝试使用 CLLocation
和后台任务来实现,但精度对我的情况来说不够。您能否解释一下,像 "moves"、"runkeeper"、"endmondo" 这样的应用程序是如何创建我的路线的?我应该使用加速计和/或指南针来创建 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);
}
}
}
更新: 刚刚测试了我的应用程序,使用了下一个属性。
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.activityType = CLActivityTypeFitness;
self.locationManager.pausesLocationUpdatesAutomatically=NO;
在这种情况下,我在1.5小时的旅程中有大约10个已触发事件。