CLGeocoder reverseGeocodeLocation返回“kCLErrorDomain错误9”

我正在根据这篇文章开发一个具有反向地理编码function的iOS应用程序: 地理编码教程

但是,当我在模拟器上这样testing,我得到'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); } }]; 

非常感谢你。

核心位置错误代码logging在这里 。

代码值8,9和10是:

 kCLErrorGeocodeFoundNoResult, kCLErrorGeocodeFoundPartialResult, kCLErrorGeocodeCanceled 

根据显示的代码,出现错误8的最可能原因是在调用startUpdatingLocation之后立即尝试使用location ,此时可能仍然nil

通常需要几秒钟的时间才能获得当前位置,直到此时最有可能nil (导致地理编码错误8或kCLErrorGeocodeFoundNoResult )。 我不确定9代表“FoundPartialResult”的错误,但苹果的GeocoderDemo示例应用程序以同样的方式处理(“无结果”)。

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

在那里,在地理编码成功(或不是)之后,您可能需要调用stopUpdatingLocation否则每次更新用户位置时都会尝试进行地理编码。

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

事实certificate,在创build位置时,我混淆了经纬度。 以为我会添加这个东西给人们检查。