如何跟踪用户在后台的位置?

我正在寻找一个开源的应用程序或库来跟踪用户在后台的位置。 现在我正在尝试使用CLLocation和后台任务来做,但是对于我的情况来说,准确性还不够。 你能解释一下,“移动”,“跑步者”,“endmondo”等应用程序如何创build我的路线? 我应该使用加速度计还是/和指南针来创buildCLLocation背景点之间的路线?

一些代码:

 //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:Justedtesting了我的应用程序的下一个属性

 self.locationManager.distanceFilter = kCLDistanceFilterNone; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest; self.locationManager.activityType = CLActivityTypeFitness; self.locationManager.pausesLocationUpdatesAutomatically=NO; 

在这种情况下,我有大约10个小时的行程中发射的事件

使用

kCLLocationAccuracyBestForNavigation

检查这个 。

您需要在“Info.plist”文件中添加值“location”(应用程序注册位置更新)的键UIBackgroundModes (array)。 你可以在这里检查所有的背景模式。

所以你的应用使用位置服务。 然后请阅读位置感知编程指南 。

您需要对Info.plist进行一些更改:

  • 如果您的应用程序依赖位置服务正常工作,请将位置服务添加到UIRequiredDeviceCapabilities
  • 如果您的应用需要GPS硬件,请将GPS添加到UIRequiredDeviceCapabilities
  • 如果您需要在后台运行您的应用程序超过10分钟,请将位置添加到UIBackgroundModes。 然后,您的位置经理将在超过10分钟的限制内提供地点信息。
  • 你还应该设置NSLocationUsageDescription(也可以是本地化的)

在后台获取位置事件

如果您的应用程序需要位置更新,无论该应用程序处于前台还是后台,都有多种select。 首选方法是使用重要位置更改服务在适当的时间唤醒您的应用程序以处理新事件。 但是,如果您的应用需要使用标准位置服务,则可以将您的应用声明为需要后台位置服务。

只有在没有这些服务会削弱其运营能力的情况下,应用程序才会请求后台定位服务。 此外,任何请求后台定位服务的应用程序都应该使用这些服务为用户提供切实的好处。 例如,一个转弯的导航应用程序可能是后台定位服务的候选人,因为它需要跟踪用户的位置,并在下次轮到时进行报告。

你的问题是后台处理程序。 删除它,并在plist文件中启用GPS背景模式。 那么你应该始终获得全功率GPS。

设置属性pausesLocationUpdatesAutomatically=NO

这是ios6中的新function。

从CLLocationManager :

允许位置pipe理器暂停更新可以在不牺牲位置数据的情况下提高目标设备上的电池寿命。 当此属性设置为YES时,位置pipe理器会在位置数据不太可能改变的时候暂停更新(并closures适当的硬件)。 例如,如果用户在使用导航应用程序时停止了食物,位置pipe理器可能暂停更新一段时间。 您可以通过为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 

在这些方法中,你将得到更新的位置。 使用它,如你所愿。 每次更新这些方法的位置都会得到调用。

你不要求代码。 你问:“我正在寻找一个开源的应用程序或库”

它可以帮助你访问这个网站。

希望它可以帮助你,

也是一个教程 。

这是我的解决scheme,

声明实例variables:

 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; } 

免责声明:我在Cintric工作

我们也希望能够在后台准确地追踪用户位置(甚至在应用程序被杀害后)。 我们花了很长时间解决这个问题,特别是关注电池的消耗。

我们发布了一篇关于如何解决这个问题的好博客文章 。 并且还提供了一个拖放SDK解决scheme。 这不是开源的,但可以免费整合:

您可以下载.framework文件,将其拖到您的项目中,并用一行代码进行初始化: [CintricFind initWithApiKey:@"YOUR_API_KEY_HERE" andSecret:@"YOUR_SECRET_HERE"];

您可以免费获得一个API密钥。 有文件解释这里的一切。