开始进行反向地理编码,_geocoder实例变量初始化OK,但没有数据传递给_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
答案 0 :(得分:0)
kCLErrorDomain错误8表示Apple geoloc服务器对提供的位置一无所知。进行反向地址解析时:lat / long没有地址匹配,进行正向地址解析时:未知地址链接到lat / long