ios - 当应用程序被杀死时,位置不会在行走时更新

标签 ios objective-c core-location cllocationmanager appdelegate

我有一项功能,当应用程序被终止或终止时,我应该获得位置的纬度和经度。

当我骑车时,当应用程序被终止/终止时,我会获得纬度和经度。

但是当我开始步行几分钟后,我无法在应用程序终止/终止时获得经纬度。

下面是我的逻辑。

+ (CLLocationManager *)sharedLocationManager {
static CLLocationManager *_locationManager;

@synchronized(self) {
    if (_locationManager == nil) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        _locationManager.allowsBackgroundLocationUpdates = YES;
        _locationManager.pausesLocationUpdatesAutomatically = NO;
        _locationManager.activityType = CLActivityTypeOther;
        _locationManager.distanceFilter = kCLDistanceFilterNone;
    }
}
      return _locationManager; 
}

 - (id)init {
    if (self==[super init]) {
    //Get the share model and also initialize myLocationArray
    self.shareModel = [LocationShareModel sharedModel];
    self.shareModel.myLocationArray = [[NSMutableArray alloc]init];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
}
    return self;
}

-(void)applicationEnterBackground{
CLLocationManager *locationManager = [LocationTracker sharedLocationManager];
locationManager.delegate = self;
[locationManager stopMonitoringSignificantLocationChanges];

if(IS_OS_8_OR_LATER) {
    [locationManager requestAlwaysAuthorization];
}
[locationManager startMonitoringSignificantLocationChanges];

//Use the BackgroundTaskManager to manage all the background Task
self.shareModel.bgTask = [BackgroundTaskManager sharedBackgroundTaskManager];
[self.shareModel.bgTask beginNewBackgroundTask];
}

上面是我从 appdelegate 调用的 Locationmanager 类

在 AppDelegate.m 文件中,我这样写

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

 UIAlertView * alert;

//We have to make sure that the Background App Refresh is enable for the Location updates to work in the background.
if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied){

    alert = [[UIAlertView alloc]initWithTitle:@""
                                      message:@"The app doesn't work without the Background App Refresh enabled. To turn it on, go to Settings > General > Background App Refresh"
                                     delegate:nil
                            cancelButtonTitle:@"Ok"
                            otherButtonTitles:nil, nil];
    [alert show];

}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted){

    alert = [[UIAlertView alloc]initWithTitle:@""
                                      message:@"The functions of this app are limited because the Background App Refresh is disable."
                                     delegate:nil
                            cancelButtonTitle:@"Ok"
                            otherButtonTitles:nil, nil];
    [alert show];

} else{
    self.locationTracker = [[LocationTracker alloc]init];
    [self.locationTracker startLocationTracking];

        //Send the best location to server every 60 seconds
        //You may adjust the time interval depends on the need of your app.
    if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {

        NSTimeInterval time = 10;
        self.locationUpdateTimer =
        [NSTimer scheduledTimerWithTimeInterval:time
                                         target:self
                                       selector:@selector(updateLocation)
                                       userInfo:nil
                                        repeats:YES];
    }


}

return YES;
}

有什么建议吗??谢谢...

最佳答案

您要求的是“SignificantLocationChanges”。我对 API 和相关区域监控的研究和经验是,由于它使用手机信号塔而不是 GPS,因此分辨率非常粗糙。我们尝试在 30 米处使用它,但它不稳定。我们把它撞到 100 米,它更好了。似乎大多数人建议 200 米用于区域监控,那么步行不太可能在一段时间内触发“SignificantLocationChange”也就不足为奇了,因为 200 米是几分钟内步行的长距离。

关于ios - 当应用程序被杀死时,位置不会在行走时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40278141/

相关文章:

iOS Swift 将 GPS 坐标存储到数组中

ios - 如何获得在不同 iPhone 模拟器中使用自动布局的 scrollView 实际大小

ios - iTunesConnect应用程序的主要语言更改无法传播

objective-c - NSThread detachNewThreadSelector 在主线程上执行

ios - CLLocationManager 总是崩溃并返回 nil 值

ios4 - 找不到 CLLocationManager startUpdatingLocation

ios - 如何从iPhone/iPad的一个屏幕显示多个EAGLView?

ios - 如何使用核心数据来构建我的模型对象

ios - React Native AppDelegate.m 没有地方改localhost

objective-c - 单击项目时关闭 NSTokenField 完成列表?