CLGeocoder reverseGeocodeLocation返回'kCLErrorDomain错误9'

goo*_*nsu 12 core-location reverse-geocoding ios

我正在根据本文开发一个具有反向地理编码功能的iOS应用程序:地理编码教程

但是当我在模拟器上测试时,我得到'kCLErrorDomain错误9'.我搜索了很多,只有错误0或1而不是9.

这是我的代码viewDidLoad:

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 80.0;
[self.locationManager startUpdatingLocation];

CLGeocoder *geocoder = [[[CLGeocoder alloc] init] autorelease];
[geocoder reverseGeocodeLocation:self.locationManager.location 
   completionHandler:^(NSArray *placemarks, NSError *error) {
       NSLog(@"reverseGeocodeLocation:completionHandler: Completion Handler called!");

       if (error){
           NSLog(@"Geocode failed with error: %@", error);            
           return;

       }

       if(placemarks && placemarks.count > 0)

       {
           //do something   
           CLPlacemark *topResult = [placemarks objectAtIndex:0];
           NSString *addressTxt = [NSString stringWithFormat:@"%@ %@,%@ %@", 
                                   [topResult subThoroughfare],[topResult thoroughfare],
                                   [topResult locality], [topResult administrativeArea]];
           NSLog(@"%@",addressTxt);
       }
   }];
Run Code Online (Sandbox Code Playgroud)

非常感谢你.

小智 17

此处记录了核心位置错误代码.

代码值8,9和10是:

kCLErrorGeocodeFoundNoResult,
kCLErrorGeocodeFoundPartialResult,
kCLErrorGeocodeCanceled
Run Code Online (Sandbox Code Playgroud)

根据显示的代码,您收到错误8的最可能原因是它location在调用之后立即尝试使用startUpdatingLocation它可能仍然存在nil.

获取当前位置通常需要几秒钟,并且很可能nil直到那时(导致地理编码错误8或kCLErrorGeocodeFoundNoResult).我不确定"FoundPartialResult"的错误代码9是什么意思,但Apple的GeocoderDemo示例应用程序对待两种方式("无结果").

尝试将地理编码代码(startUpdatingLocation调用后的所有代码)移动到委托方法locationManager:didUpdateToLocation:fromLocation:.位置管理器将在实际拥有位置时调用该委托方法,然后才能安全使用location.

在地理编码成功(或不成功)之后,您可能想要打电话,stopUpdatingLocation否则每次更新用户位置时都会尝试地理编码.

在尝试对其进行地理编码之前,您可能还需要检查收到位置的准确度(newLocation.horizontalAccuracy)和年龄(newLocation.timestamp).