【问题标题】:kCLErrorDomain Code=8 "The operation couldn’t be completed.kCLErrorDomain Code=8 "操作无法完成。
【发布时间】:2014-01-04 12:33:31
【问题描述】:

我开始进行反向地理编码,_geocoder 实例变量初始化成功,但没有数据传递给 _placemark 对象。这是错误日志:

2013-12-16 14:00:12.040 MyLocations[10555:70b] *** Going to geocode
2013-12-16 14:00:12.041 MyLocations[10555:70b] *** Found placemarks: (null), error: Error Domain=kCLErrorDomain Code=8 "The operation couldn’t be completed. (kCLErrorDomain error 8.)"

我假设 locationManager:didFailWithError: 方法被调用,但不理解错误代码。

这是 CurrentLocationViewController.m 的代码

#import "CurrentLocationViewController.h"

@interface CurrentLocationViewController ()

@end

@implementation CurrentLocationViewController {
    CLLocationManager *_locationManager;
    CLLocation *_location;

    BOOL _updatingLocation;
    NSError *_lastLocationError;

    CLGeocoder *_geocoder;
    CLPlacemark *_placemark;
    BOOL _performingReverseGeocoding;
    NSError *_lastGeocodingError;
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
    if((self = [super initWithCoder:aDecoder])) {
        _locationManager = [[CLLocationManager alloc] init];
        _geocoder = [[CLGeocoder alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self updateLabels];
    [self configureGetButton];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)getLocation:(id)sender
{
    if(_updatingLocation) {
        [self stopLocationManager];
    } else {
        _location = nil;
        _lastLocationError = nil;

        [self startLocationManager];
    }
    [self updateLabels];
    [self configureGetButton];

}

#pragma mark - CLLocationManagerDelegate

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError %@", error);

    if (error.code == kCLErrorLocationUnknown) {
        return;
    }

    [self stopLocationManager];
    _lastLocationError = error;

    [self updateLabels];
    [self configureGetButton];


}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *newLocation = [locations lastObject];

    if ([newLocation.timestamp timeIntervalSinceNow] <- 5.0) {
        return;
    }

    if (newLocation.horizontalAccuracy < 0) {
        return;
    }

    if (!_performingReverseGeocoding) {
        NSLog(@"*** Going to geocode");

        _performingReverseGeocoding = YES;

        [_geocoder reverseGeocodeLocation:_location completionHandler:^(NSArray *placemarks, NSError *error) {
            NSLog(@"*** Found placemarks: %@, error: %@", placemarks, error);

            _lastGeocodingError = error;
            if (error == nil && [placemarks count] > 0) {
                _placemark = [placemarks lastObject];
            } else {
                _placemark = nil;
            }

            _performingReverseGeocoding = NO;
            [self updateLabels];
        }];

    }
}

-(void)updateLabels
{
    if (_location != nil) {
        self.latitudeLabel.text = [NSString stringWithFormat:@"%.8f", _location.coordinate.latitude];
        self.longitudeLabel.text = [NSString stringWithFormat:@"%.8f", _location.coordinate.longitude];
        self.tagButton.hidden = NO;
        self.messageLabel.text = @"";
    } else {
        self.latitudeLabel.text = @"";
        self.longitudeLabel.text = @"";
        self.addressLabel.text = @"";
        self.tagButton.hidden = YES;

        NSString *statusMessage;
        if (_lastLocationError == nil) {
            if ([_lastLocationError.domain isEqualToString:kCLErrorDomain] && _lastLocationError.code == kCLErrorDenied) {
                statusMessage = @"Location Services Disabled";
            } else {
                statusMessage = @"Error Getting Location";
            }
        } else if (![CLLocationManager locationServicesEnabled]) {
            statusMessage = @"Location Services Disabled";
        } else if (_updatingLocation) {
            statusMessage = @"Searching...";
        } else {
            statusMessage = @"Press the Button to Start";
        }

        self.messageLabel.text = statusMessage;
        }
}

-(void)configureGetButton
{
    if (_updatingLocation) {
        [self.getButton setTitle:@"Stop"
                        forState:UIControlStateNormal];
    } else {
        [self.getButton setTitle:@"Get My Location"
                        forState:UIControlStateNormal];
    }
}

-(void)startLocationManager
{
    if ([CLLocationManager locationServicesEnabled]) {
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        [_locationManager startUpdatingLocation];
        _updatingLocation = YES;
    }
}

-(void)stopLocationManager
{
    if (_updatingLocation) {
        [_locationManager stopUpdatingLocation];
        _locationManager.delegate = nil;
        _updatingLocation = NO;
    }
}

@end

【问题讨论】:

  • 在 didUpdateLocations 中,使用 _location 调用 reverseGeocodeLocation,但我看不到 _location 的设置位置。如果它是 nil,那可能会给你这个错误。
  • 谢谢。我添加了“_location = newLocations;”到 didUpdateLocations 方法,一切正常。

标签: ios xcode cllocationmanager


【解决方案1】:

kCLErrorDomain 错误 8 表示 Apple geoloc 服务器对提供的位置一无所知。进行反向地理编码时:lat/long 没有地址匹配,当进行正向地理编码时:地址未知链接到 lat/long

【讨论】:

    猜你喜欢
    • 2015-05-19
    • 2010-11-24
    • 2011-08-27
    • 2013-10-21
    • 2015-06-03
    • 2014-03-04
    • 2018-10-24
    • 2012-12-24
    • 1970-01-01
    相关资源
    最近更新 更多