【问题标题】:How to get accurate location update when application is killed?应用程序被杀死时如何获得准确的位置更新?
【发布时间】:2015-03-25 06:12:13
【问题描述】:

我正在开发一个基于位置的应用程序,用户可以在其中存储多个位置,当用户经过该存储位置附近时,应用程序将通过本地通知通知用户。

我使用以下代码来实现此功能:


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

通过使用上面的代码,即使应用程序被用户杀死,我也能够获得位置,但没有应有的准确。有时我没有得到位置,有时我在通过存储的位置后得到位置。我需要在用户输入特定半径时准确显示位置通知。

请帮我解决这个问题。

提前致谢。

【问题讨论】:

  • 您是否在开车或在其他车辆上或步行时测试此应用程序?
  • 现在,我正在使用 CLLocationRegion 以更好的方式获取基于位置的通知。
  • @MehulS 请发布您的答案

标签: ios location core-location cllocationmanager uilocalnotification


【解决方案1】:
猜你喜欢
  • 2017-03-09
  • 2016-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
  • 2015-08-04
相关资源
最近更新 更多