编辑:好的,我刚刚注意到问题仅在iOS 7之外。但是,我仍然无法解决它,因此,我支持我尝试使用波纹管。谢谢大家!

嗨!我正在编程一个导航器应用程序,因此我需要尽可能地更新用户位置。我有两个使用CLLocation管理器的视图控制器。在它们两个中,我都添加了以下行:

#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);
    }
}

对于第二个视图。如您所见,我拼命尝试将旧的didUpdateToLocation替换为didUpdateLocations。
#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];

被评论。嗯,问题使我发疯了,因为上面的代码在第一个视图控制器中起了魅力,并按我的预期定期更新。但是在第二种视图中,它只能在第一次使用,而永远不会更新。我在不同的时间对这些方法进行了编程,所以我不记得我是否对第一个视图做了什么而对第二个视图没有做。但是它们在同一个应用程序中,因此我认为库或权限没有问题。欢迎任何帮助或提示,谢谢!

最佳答案

首先,如果要进行导航,则应该注册重大更改通知。阅读该here上的文档。效率更高。不使用时必须将其关闭,但这要好得多。对它的支持可以一直追溯到iOS4。这肯定是99%的用户。

关于ios - objective-c ,iOS 7-CLLocationManager:didUpdateLocations仅被调用一次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21768442/

10-08 21:25