【问题标题】:Location not updating while walking, when app is killed走路时位置不更新,当应用程序被杀死时
【发布时间】:2017-03-09 18:08:47
【问题描述】:

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

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

但是当我开始步行几分钟时,我不会在应用程序被终止/终止时获得纬度和经度。

以下是我的逻辑。

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

任何提示??谢谢...

【问题讨论】:

  • 您是否在后台模式中进行了相关更改?
  • @ReshmiMajumder :是的,我已经为位置和后台获取启用了后台模式
  • 步行时更新位置需要互联网或 3G 吗? @ReshmiMajumder
  • 是的,你需要 3G
  • 我有 3G...但走路时仍然无法工作..@ReshmiMajumder

标签: ios objective-c core-location cllocationmanager appdelegate


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 2018-05-26
  • 1970-01-01
  • 2016-04-04
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 2013-09-19
相关资源
最近更新 更多