CLGeocoder的reverseGeocodeLocation返回“kCLErrorDomain error 9”

13

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

但是当我在模拟器上进行测试时,出现了“kCLErrorDomain error 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);
       }
   }];

非常感谢。


模拟器现在返回错误代码8,但我并没有更改代码。 - goofansu
1
CLLocationManager和CLGeocoder在模拟器上无法工作。它只能在设备调试或测试时显示结果。在模拟器上会显示错误。 - Mobile App Dev
这在我的iPhone 4设备上运行得非常好。谢谢! - gstroup
你必须确保在模拟器中模拟位置。 - Daniel
2个回答

18

核心定位错误代码在此处记录

代码值为8、9和10:

kCLErrorGeocodeFoundNoResult,
kCLErrorGeocodeFoundPartialResult,
kCLErrorGeocodeCanceled

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

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

尝试将地理编码代码(startUpdatingLocation调用后的所有代码)移动到委托方法locationManager:didUpdateToLocation:fromLocation:中。只有当位置管理器实际上具有位置时,它才会调用该委托方法,此时才可以安全地使用location

在那里,在地理编码成功(或失败)后,您可能希望调用stopUpdatingLocation,否则它将尝试在每次用户位置更新时进行地理编码。

在尝试进行地理编码之前,您可能还想检查接收到的位置的准确性 (newLocation.horizontalAccuracy) 和时间戳 (newLocation.timestamp)。


谢谢。根据您的建议,我移动了我的代码后,即使我将我的CCLocation设置为“CLLocation *location = [[CLLocation alloc] initWithLatitude:37.78583400 longitude:-122.40641700];”,它仍然显示错误9。也许模拟器上有一些错误,我打算在我的设备上进行测试。 - goofansu

1
我发现在创建位置时混淆了经度和纬度。我觉得这可以作为供人们检查的内容。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接