ios – CLGeocoder reverseGeocodeLocation返回’kCLErrorDomain错误9′

我正在开发一个具有反向地理编码功能的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);
       }
   }];

非常感谢你.

解决方法

核心位置错误代码documented here.

代码值8,9和10是:

kCLErrorGeocodeFoundnoresult,kCLErrorGeocodeFoundPartialResult,kCLErrorGeocodeCanceled

根据显示代码,最有可能的原因,你会得到错误8是它试图在调用startUpdatingLocation之后立即使用位置,此时它可能仍然为零.

通常需要几秒钟才能获得当前的位置,并且很可能在此之前为零(导致地理编码错误8或kCLErrorGeocodeFoundnoresult).我不知道“FoundPartialResult”是什么错误代码9,但是Apple的GeocoderDemo示例应用程序同样处理(“No Result”).

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

在地理编码成功(或不)​​之后,您可能想要调用stopUpdatingLocation,否则它将在每次更新用户位置时尝试地理编码.

在尝试对其进行地理编码之前,您可能还需要检查接收到的位置的准确性(newLocation.horizo​​ntalAccuracy)和age(newLocation.timestamp).

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...