CLGeocoder reverseGeocodeLocation returns 'kCLErrorDomain error 9'

三世轮回 提交于 2019-12-03 07:27:45

问题


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

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.

Here is my code in 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.


回答1:


The Core Location error codes are documented here.

Code values 8, 9, and 10 are:

kCLErrorGeocodeFoundNoResult,
kCLErrorGeocodeFoundPartialResult,
kCLErrorGeocodeCanceled

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.

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").

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.

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.

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




回答2:


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.



来源:https://stackoverflow.com/questions/9526541/clgeocoder-reversegeocodelocation-returns-kclerrordomain-error-9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!