如何在后台跟踪用户位置? [英] How to track user location in background?

查看:326
本文介绍了如何在后台跟踪用户位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

一些代码:

  //位置管理器init 
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self; (CLLocationManager *)管理器didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)(bLocation *) oldLocation {
$ b $ 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

检查


I'm looking for an open source app or library to track user location in the background. Now I'm trying to do it with CLLocation and background tasks, but accuracy is not enough for my case. Could you explain, how apps, like "moves", "runkeeper", "endmondo", creates my route? Should I use Accelerometer or/and compass to create a route between CLLocation background points?

Some code:

//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 tested my app with next properties

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

In this case I have about 10 fired events during 1,5 hour trip

解决方案

Use

kCLLocationAccuracyBestForNavigation

check this.

这篇关于如何在后台跟踪用户位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆