CLGeocoder reverseGeocodeLocation returns 'kCLErrorDomain error 9'
Asked Answered
O

2

13

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.

Officialdom answered 2/3, 2012 at 0:52 Comment(4)
The simulator returns error 8 now while I did nothing to my code.Officialdom
CLLocationManager, CLGeocoder Will not work on simulator. It will display results only on debugging on Device or testing on device. On Simulator it will show error.Plumbum
This works like a charm on my iPhone 4 device. Thanks!Cinerarium
You must make sure you're simulating location in the simulatorLandon
S
18

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.

Shively answered 2/3, 2012 at 2:54 Comment(1)
Thank you. After move my code according to your advice, it shows error 9 even if I set my CCLocation as CLLocation *location = [[CLLocation alloc] initWithLatitude:37.78583400 longitude:-122.40641700];. Maybe there is something error on the simulator, I'm going to test it on my device.Officialdom
J
1

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.

Judaica answered 27/3, 2013 at 3:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.