gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:12 25 4
gpt4 key购买 nike

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

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

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

下面是我的逻辑。

+ (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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com