繁体   English   中英

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

[英]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个被解雇的事件

使用

kCLLocationAccuracyBestForNavigation

检查一下

您需要在“Info.plist”文件中添加键值为“location”的关键UIBackgroundModes (数组)(app注册位置更新)。 您可以在此处查看所有背景模式。

所以你的应用程序使用位置服务 那么请阅读位置感知编程指南

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

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

在后台获取位置事件

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

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

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

设置属性pausesLocationUpdatesAutomatically=NO

这是ios6中的新功能。

来自CLLocationManager

允许位置管理器暂停更新可以在不牺牲位置数据的情况下延长目标设备的电池寿命。 当此属性设置为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;
}

免责声明:我为Cintric工作

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

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

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

您可以免费获得API密钥。 有文档解释一切在这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM