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

时间:2013-07-04 08:51:53

标签: ios ios6 geolocation core-location cllocationdistance

我正在寻找一个开源应用或库来跟踪后台的用户位置。现在我正在尝试使用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个被解雇的事件

8 个答案:

答案 0 :(得分:7)

使用

  

kCLLocationAccuracyBestForNavigation

检查this

答案 1 :(得分:6)

您需要在“Info.plist”文件中添加值UIBackgroundModes(数组),其值为“location”(应用程序寄存器用于位置更新)。 您可以检查所有后台模式here

答案 2 :(得分:4)

因此,您的应用使用了位置服务。那么请阅读 Location Awareness Programming Guide

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

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

在后台获取位置事件

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

仅当缺少这些服务会影响其运营能力时,应用才应申请后台位置服务。此外,任何请求后台位置服务的应用程序都应使用这些服务为用户提供切实的好处。例如,逐向导航应用程序可能是后台位置服务的候选者,因为它需要跟踪用户的位置并在下次转弯时报告。

答案 3 :(得分:3)

您的问题是后台处理程序。将其删除并在plist文件中启用gps后台模式。那么你应该一直得到全功率的GPS。

设置属性pausesLocationUpdatesAutomatically=NO

这是ios6中的新功能。

来自CLLocationManager

  

允许位置管理器暂停更新可以改善电池电量   在不牺牲位置数据的情况下在目标设备上生存。当这个   property设置为YES,位置管理器暂停更新(和   在位置数据时有时关闭适当的硬件   不太可能改变。例如,如果用户停下来吃饭的话   使用导航应用程序,位置管理器可能暂停a的更新   一段的时间。您可以帮助确定何时暂停   通过为activityType属性赋值来更新位置。

     

此属性的默认值为YES。

要进行分析,请将这些方法添加到LocationManager委托:

- (void)locationManagerDidPauseLocationUpdates:(CLLocationManager *)manager {
     NSLog(@"locMan: locationManagerDidPauseLocationUpdates");
}

- (void)locationManagerDidResumeLocationUpdates:(CLLocationManager *)manager {
    NSLog(@"locMan: locationManagerDidResumeLocationUpdates");
}

答案 4 :(得分:2)

您可以在VC中设置监控位置内容,如下所示

在viewDidLoad方法中执行如下

CLLocationManager locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;(Accuracy according to your need)
[locationManager startUpdatingLocation];

比必须覆盖CLLocationManagerDelegate协议的两个可选委托方法

for iOS6 +

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{}

和iOS 2到6

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

在这些方法中,您将获得更新的位置。根据需要使用它。 每次位置更新这些方法都会得到电话。

答案 5 :(得分:1)

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

可以帮助您访问this网站。

希望它可以帮到你,

也是tutorial

答案 6 :(得分:0)

以下是我的解决方案,

声明实例变量:

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

答案 7 :(得分:-3)

免责声明:我为Cintric工作

我们还希望能够在后台准确跟踪用户位置(即使应用程序被杀死后也是如此)。我们花了很长时间解决这个问题,特别是关注电池耗尽。

我们发布了一篇很棒的blog post关于我们如何解决它的信息。并且还提供拖放SDK解决方案。它不是开源的,但您可以免费集成它:

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

您可以免费获得API密钥。有文档解释了所有内容here

相关问题