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.

4
  • The simulator returns error 8 now while I did nothing to my code.
    – goofansu
    Commented Mar 2, 2012 at 1:09
  • 1
    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. Commented Oct 16, 2012 at 5:50
  • This works like a charm on my iPhone 4 device. Thanks!
    – gstroup
    Commented Jan 3, 2013 at 17:30
  • You must make sure you're simulating location in the simulator
    – Daniel
    Commented Sep 11, 2014 at 12:45

2 Answers 2

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.

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.
    – goofansu
    Commented Mar 2, 2012 at 3:24
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.