简体   繁体   English

当应用程序被杀死时,如何获取准确的位置更新?

[英]How to get accurate location update when application is killed?

I'm developing a location based application where user can stored number of locations and when user passed near by that stored location, application will notify user by local notification. 我正在开发一个基于位置的应用程序,用户可以在其中存储多个位置,并且当用户经过该存储位置附近时,应用程序将通过本地通知来通知用户。

I've used following code to achieve this feature: 我使用以下代码来实现此功能:


-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied){

         [self showAlertWithTitle:@"" andMessage:@"The app doesn't work without the Background App Refresh enabled. To turn it on, go to Settings > General > Background App Refresh."];
  }else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted){

    [self showAlertWithTitle:@"" andMessage:@"The functions of this app are limited because the Background App Refresh is disable."];
  } else {
    if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {

        // This "afterResume" flag is just to show that he receiving location updates
        self.shareModel.afterResume = YES;

        self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
        self.shareModel.anotherLocationManager.delegate = self;
        self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; //kCLLocationAccuracyBestForNavigation
        self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;
        [self.shareModel.anotherLocationManager startUpdatingLocation];
                 self.shareModel.anotherLocationManager.pausesLocationUpdatesAutomatically = YES;


        if(IS_OS_8_OR_LATER) {
            if ([self.shareModel.anotherLocationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [self.shareModel.anotherLocationManager requestWhenInUseAuthorization];
            }
            [self.shareModel.anotherLocationManager requestAlwaysAuthorization];
        }

        [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
    }
  }
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

CLLocation *currentLocation = [locations lastObject];

latitudeTemp = currentLocation.coordinate.latitude;
longitudeTemp = currentLocation.coordinate.longitude;

[self locationChangeLogic];    

//If the timer still valid, return it (Will not run the code below)
if (self.shareModel.timer)
    return;

self.shareModel.bgTask = [BackgroundTaskManager sharedBackgroundTaskManager];
[self.shareModel.bgTask beginNewBackgroundTask];

//Restart the locationMaanger after 30 Seconds
self.shareModel.timer = [NSTimer scheduledTimerWithTimeInterval:30
                                                         target:self
                                                       selector:@selector (restartLocationUpdates)
                                                       userInfo:nil
                                                        repeats:YES];

//Will only stop the locationManager after 10 seconds, so that we can get some accurate locations
//The location manager will only operate for 10 seconds to save battery
NSTimer * delay10Seconds;
delay10Seconds = [NSTimer scheduledTimerWithTimeInterval:10
                                                  target:self
                                                selector:@selector(stopLocationDelayBy10Seconds)
                                                userInfo:nil
                                                 repeats:YES];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
      [self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.


//Remove the "afterResume" Flag after the app is active again.
self.shareModel.afterResume = NO;

if(self.shareModel.anotherLocationManager)
    [self.shareModel.anotherLocationManager stopMonitoringSignificantLocationChanges];

self.shareModel.anotherLocationManager = [[CLLocationManager alloc]init];
self.shareModel.anotherLocationManager.delegate = self;
self.shareModel.anotherLocationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; //kCLLocationAccuracyBestForNavigation;
self.shareModel.anotherLocationManager.activityType = CLActivityTypeOtherNavigation;
[self.shareModel.anotherLocationManager startUpdatingLocation];

if(IS_OS_8_OR_LATER) {
    if ([self.shareModel.anotherLocationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.shareModel.anotherLocationManager requestWhenInUseAuthorization];
    }
    [self.shareModel.anotherLocationManager requestAlwaysAuthorization];
}
[self.shareModel.anotherLocationManager startMonitoringSignificantLocationChanges];
}

By using above code, i'm able to get location even application killed by User but not as accurate as it should be. 通过使用上面的代码,我甚至可以获得位置被用户杀死的应用程序,但不够准确。 Sometime i didn't get location and sometime i get location after passing that stored location. 有时我没有获得位置,有时我在通过存储的位置后获得了位置。 I need to show location notification exactly when user enter in that specific radius. 当用户输入该特定半径时,我需要准确显示位置通知。

Please help me for solving this issue. 请帮助我解决此问题。

Thanks in advance. 提前致谢。

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

相关问题 当应用程序被杀死或后台时如何更新估计位置的设备位置 - how to update device position in estimote indoor location when application is killed or Background 当应用被用户杀死时如何重新启动位置更新? - How to restart the location update when the app is killed by user? 当应用程序处于后台或被杀时,如何定期获取新位置 - How to get periodicaly new location when app is in background or killed 即使应用程序被杀死/终止,位置更新 - Location update even when app is killed/terminated CLLocationManager更新位置不正确 - CLLocationManager update location is not accurate 如何在 ios swift 中获得准确的当前位置,准确度为 5 米 - How To get Accurate current location in ios swift 5 meters accurate 使用 VOIP 杀死应用程序时如何获取呼叫通知 - How to get the call notification when application is killed using VOIP 即使应用程序被杀死,如何获得设备锁定和解锁? - How to get device lock and unlock even when the application was killed? 在iOS中获取准确的位置 - Get accurate location in iOS 如果我开启了位置服务,那么进入后台模式后如何杀死我的应用? - If I have location services turned on then how can my app get killed when it enters background mode?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM