CLGeocoder reverseGeocodeLocation返回'kCLErrorDomain错误9'

时间:2012-03-02 00:52:01

标签: ios core-location reverse-geocoding

根据这篇文章,我正在开发一款具有反向地理编码功能的iOS应用:geocoding tutorial

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

非常感谢。

2 个答案:

答案 0 :(得分:17)

核心位置错误代码为documented here

代码值8,9和10是:

kCLErrorGeocodeFoundNoResult,
kCLErrorGeocodeFoundPartialResult,
kCLErrorGeocodeCanceled

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

获取当前位置通常需要几秒钟,直到那时它很可能是nil(导致地理编码错误8或kCLErrorGeocodeFoundNoResult)。我不确定“FoundPartialResult”的错误代码9是什么意思,但Apple的GeocoderDemo示例应用程序以相同的方式处理(“无结果”)。

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

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

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

答案 1 :(得分:1)

事实证明,在创建位置时我混合了经度和纬度。以为我会把它添加为人们检查的东西。