2012-03-02 54 views
12

我發展與反向地理編碼的iOS應用功能根據這篇文章:geocoding tutorialCLGeocoder reverseGeocodeLocation返回「kCLErrorDomain錯誤9」

但是當我測試這樣的模擬器,我得到「kCLErrorDomain錯誤9」。我搜索了很多,唯一的錯誤0或1不是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); 
     } 
    }]; 

非常感謝你。

+0

模擬器現在返回錯誤8,而我什麼也沒有做到我的代碼。 – goofansu 2012-03-02 01:09:25

+1

CLLocationManager,CLGeocoder不能在模擬器上工作。它僅在Device上進行調試或在設備上進行測試時顯示結果。在模擬器上它會顯示錯誤。 – 2012-10-16 05:50:45

+0

這在我的iPhone 4設備上很有魅力。謝謝! – gstroup 2013-01-03 17:30:10

回答

16

核心位置錯誤代碼是documented here

碼值8,9,和10:

kCLErrorGeocodeFoundNoResult, 
kCLErrorGeocodeFoundPartialResult, 
kCLErrorGeocodeCanceled 

根據顯示的代碼,你會得到錯誤8最可能的​​原因是,它試圖用location處調用startUpdatingLocation後立即時間可能仍然是nil

通常需要幾秒鐘才能獲取當前位置,直到此時(很可能導致地理編碼錯誤8或kCLErrorGeocodeFoundNoResult),它很可能是nil。我不確定「FoundPartialResult」代碼9意味着什麼,但Apple的GeocoderDemo示例應用程序以同樣的方式對待(如「無結果」)。

嘗試將地理編碼代碼(調用startUpdatingLocation後的所有代碼)移至代理方法locationManager:didUpdateToLocation:fromLocation:。位置管理員在實際有位置時會調用該委託方法,只有這樣才能安全地使用location

在那裏,在地理編碼成功(或不)​​之後,您可能需要撥打stopUpdatingLocation,否則每次更新用戶位置時都會嘗試進行地理編碼。

在嘗試對其進行地理編碼之前,您可能還需要檢查接收位置的準確性(newLocation.horizontalAccuracy)和年齡(newLocation.timestamp)。

+0

謝謝。根據您的建議移動我的代碼後,即使將我的CCLocation設置爲CLLocation * location = [[CLLocation alloc] initWithLatitude:37.78583400 longitude:-122.40641700];',它也會顯示錯誤9。也許在模擬器上有錯誤,我要在我的設備上測試它。 – goofansu 2012-03-02 03:24:58

1

事實證明,在創建位置時,我混淆了結構和緯度。以爲我會將此添加爲人們檢查的內容。

相關問題