CLGeocoder reverseGeocodeLocation返回'kCLErrorDomain错误9' [英] CLGeocoder reverseGeocodeLocation returns 'kCLErrorDomain error 9'

查看:519
本文介绍了CLGeocoder reverseGeocodeLocation返回'kCLErrorDomain错误9'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

这是我的代码 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,9和10是:

Code values 8, 9, and 10 are:

kCLErrorGeocodeFoundNoResult,
kCLErrorGeocodeFoundPartialResult,
kCLErrorGeocodeCanceled

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

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

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

It usually takes a few seconds to obtain the current location and it will most likely be nil until 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").

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

Try moving the geocoding code (all the code after the startUpdatingLocation call) 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.

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

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

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

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

这篇关于CLGeocoder reverseGeocodeLocation返回'kCLErrorDomain错误9'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆