问题
Am starting to do reverse geocoding, and the _geocoder instance variable gets initialized OK, but no data gets passed to the _placemark object. Here is the error log:
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.)"
Am assuming that the locationManager:didFailWithError: method is getting called, but don't understand the error code.
Here is the code for 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
回答1:
kCLErrorDomain error 8 means Apple geoloc servers know nothing about the provided location. When doing reverse geocoding: lat/long has no address match, when forward geocoding: the address is not known to link to lat/long
来源:https://stackoverflow.com/questions/20622432/kclerrordomain-code-8-the-operation-couldn-t-be-completed