Objective C,iOS 7 – CLLocationManager:didUpdateLocations只被调用一次

编辑:好吧,我只是已经注意到,这个问题是独家的iOS 7。我仍然无法解决它,但是,我认为我会尝试与下面的sugestions。 谢谢大家!

¡嗨! 我正在编程一个导航器应用程序,我需要更新用户的位置,只要有可能。 我有两个使用CLLocationpipe理器的视图控制器。 在他们两个,我已经添加了这一行:

#import <CoreLocation/CoreLocation.h> 

然后将其添加到接口声明中,并将其设置为.h文件中的属性,然后进行合成:

 @property (strong, nonatomic) CLLocationManager *locationManager; 

之后,我将在viewDidLoad中启动de locationManager,如下所示:

 if(self){ locationManager = [[CLLocationManager alloc] init]; //locationManager.delegate = self; [locationManager setDelegate:self]; [locationManager setDistanceFilter:kCLHeadingFilterNone]; [locationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation]; [locationManager startUpdatingLocation]; } 

这里是我的委托方法。

对于第一个视图:

 #pragma mark - CLLocationManagerDelegate - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"didFailWithError: %@", error); UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [errorAlert show]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"didUpdateToLocation: %@", newLocation); CLLocation *currentLocation = newLocation; if (currentLocation != nil) { homeLatitude = [[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude] doubleValue]; homeLongitude = [[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude] doubleValue]; NSLog(@"Updated! -> hl = %f, hlg = %f", homeLatitude, homeLongitude); } } 

对于第二种观点。 正如你所看到的,我用didUpdateLocationsreplace了旧的didUpdateToLocation,作为一个绝望的尝试。

 #pragma mark - CLLocationManagerDelegate - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"didFailWithError: %@", error); UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [errorAlert show]; } /*- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"didUpdateToLocation: %@", newLocation); CLLocation *currentLocation = newLocation; if (currentLocation != nil) { NSLog(@"Tarzan boy"); _testLabel.text = [NSString stringWithFormat:@"Oh DAMN!!!!"]; } }*/ -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { NSLog(@"didUpdateToLocation: %@", [locations lastObject]); CLLocation *currentLocation = [locations lastObject]; if (currentLocation != nil) { currentLatitude = [[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude] doubleValue]; currentLongitude = [[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude] doubleValue]; NSLog(@"Updated! (MapWithRoutesViewController) -> hl = %f, hlg = %f", currentLatitude, currentLongitude); _testLabel.text = [NSString stringWithFormat:@"Update! -> hl = %f, hlg = %f", currentLatitude, currentLongitude]; //Aquí es donde borramos el lugar al que llegaron. if(mapView){ if(followMe){ CLLocationCoordinate2D *c = &((CLLocationCoordinate2D){.latitude = currentLatitude, .longitude = currentLongitude}); [mapView.mapView as_setCenterCoordinate:*c zoomLevel:32 animated:NO]; _ourStepper.value = [mapView.mapView as_zoomLevel]; } if([mapView getNearestDestinyDistance:currentLocation] < 150.0){ NSLog(@"Hay que eliminar el primer destino del MapView"); mapView.destinoActual++; } if([mapView getNearestPointDistance:currentLocation] > 200.0){ NSLog(@"Hay que recalcular la ruta al destinoActual"); SpinnerView *spinner = [SpinnerView loadSpinnerIntoView:self.view]; spinner.tag = 98; while (![mapView recalculateRoutesTo:currentLocation]) { //do nothin... } [spinner removeSpinner]; } } } //[locationManager stopUpdatingLocation]; } 

正如你所看到的那样

 //[locationManager stopUpdatingLocation]; 

被评论。 那么,问题是让我疯狂的是,上面的代码作为魅力在第一个视图控制器,并定期更新,如我所料。 但是第二种观点认为,这只是第一次,而且不会再更新。 我在不同的时间对这些方法进行了编程,所以我不记得是否对第一个观点做了些什么,但是我没有做到。 但他们在同一个应用程序,所以我认为没有问题的库或权限。 欢迎任何帮助或提示,谢谢!

首先,如果您正在进行导航,则应该注册重大更改通知。 去阅读这里的文档。 这是更有效率。 你不用的时候不得不把它关掉,但是好多了。 对它的支持一直回到iOS 4.这肯定是99%的用户群体。

我在我的应用程序中有类似的情况,并实现了一个处理所有位置更新的单例,然后将NSNotificaitons引发到前台线程。

  • 创build一个处理位置更新视图的单例类
  • 注册听LOCATION_CHANGE更新或者背景
  • 单身对象注册为位置pipe理器的代理

这可能不是你正在寻找的答案,但我知道它适用于我的情况。

在这里find解决scheme。 当我以这种方式启动locationManager时,locationManager会工作:

 if(self){ locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:self]; [locationManager setDistanceFilter:kCLHeadingFilterNone]; //change the desired accuracy to kCLLocationAccuracyBest [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; //SOLUTION: set setPausesLocationUpdatesAutomatically to NO [locationManager setPausesLocationUpdatesAutomatically:NO]; [locationManager startUpdatingLocation]; } 

无论如何,我会像罗布build议的那样performance。 感谢所有的帮助!