ios CLGeocoder reverseGeocodeLocation 返回“kCLErrorDomain 错误 9”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9526541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:07:38  来源:igfitidea点击:

CLGeocoder reverseGeocodeLocation returns 'kCLErrorDomain error 9'

ioscore-locationreverse-geocoding

提问by goofansu

I'm developing an iOS app with reverse geocoding features according to this article: geocoding tutorial

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

But when I test like this on simulator, I get 'kCLErrorDomain error 9'. I've searched a lot and there are only error 0 or 1 not 9.

但是当我在模拟器上进行这样的测试时,我得到了“kCLErrorDomain 错误 9”。我搜索了很多,只有错误 0 或 1 而不是 9。

Here is my code in viewDidLoad:

这是我的代码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);
       }
   }];

Thank you very much.

非常感谢。

回答by

The Core Location error codes are documented here.

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

Code values 8, 9, and 10 are:

代码值 8、9 和 10 是:

kCLErrorGeocodeFoundNoResult,
kCLErrorGeocodeFoundPartialResult,
kCLErrorGeocodeCanceled

Based on the code shown, the most likely reason you'd get error 8 is that it's trying to use locationimmediately after calling startUpdatingLocationat which time it might still be nil.

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

It usually takes a few seconds to obtain the current location and it will most likely be niluntil then (resulting in geocode error 8 or kCLErrorGeocodeFoundNoResult). I'm not sure what error code 9 means by "FoundPartialResult" but Apple's GeocoderDemo sample app treats both the same way (as "No Result").

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

Try moving the geocoding code (all the code after the startUpdatingLocationcall) to the delegate method locationManager:didUpdateToLocation:fromLocation:. The location manager will call that delegate method when it actually has a location and only then is it safe to use location.

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

There, after the geocoding is successful (or not), you may want to call stopUpdatingLocationotherwise it will try geocoding every time the user location is updated.

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

You may also want to check the accuracy (newLocation.horizontalAccuracy) and age (newLocation.timestamp) of the received location before trying to geocode it.

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

回答by Jim

It turns out I mixed up the longitute and latitude when creating the location. Thought I'd add this as something for people to check.

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