2010-12-17 26 views
0

我正在嘗試獲取用戶位置MKMapView的地址。MKReverseGeocoder不會調用代理

這裏是我的代碼:

CLLocationCoordinate2D userCoordinate = mapview.userLocation.location.coordinate; 


MKReverseGeocoder *geocoder = [[[MKReverseGeocoder alloc] initWithCoordinate:userCoordinate] autorelease]; 

geocoder.delegate = self; 
[geocoder start]; 

locationInstructions.text = @"Finding your location..."; 

NSLog(@"Started Geocoder"); 

日誌被稱爲罰款,如果我登錄NSLog(%p,geocorder.delegate);,它不會返回nil。所以我的委託沒有任何問題。

現在確定將委託方法:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error; 
{ 
    NSLog(@"Failed!"); 
} 

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark 
{ 
    NSString*str = [NSString stringWithFormat:@"%@,%@,%@,%@",[placemark thoroughfare],[placemark locality],[placemark administrativeArea],[placemark country]]; 

    NSLog(@"%@",str); 
} 

代表曾經得到所謂的無。爲什麼是這樣?我錯過了什麼?

回答

0

可能需要而不是首先autorelease地理編碼器。

按abitobvious編輯:將geocoder分配給實例變量/屬性,然後在包含對象被釋放時顯式釋放該實例變量/屬性。

+0

不會拋出一個異常,並在應用程序預先發布時崩潰嗎? – Pripyat 2010-12-17 20:39:24

+0

刪除'autorelease'確實有效。我不知道這是爲什麼發生,但它的工作! :) – Pripyat 2010-12-17 20:40:43

+0

儘管刪除autorelease「工作」,你現在有內存泄漏。請按照Apple示例應用中的模式[CurrentAddress](http://developer.apple.com/library/ios/#samplecode/CurrentAddress/Introduction/Intro.html)進行操作。 – Anna 2010-12-17 20:49:51

0

您是否在頭中實現了MKReverseGeocoderDelegate

+0

是我。也沒有警告...... – Pripyat 2010-12-17 20:38:28

+2

也許在你的標題中定義你的地理編碼器,而不是範圍? – WrightsCS 2010-12-17 20:40:08

+0

我在其中一個應用中使用了相同的方法,並使用@property和@synthesize在頭文件中定義了我的作品。 – WrightsCS 2010-12-17 20:41:26

0

您應該在實例級別定義MKReverseGeocoder *geoCoder,而不是在方法級別定義。

我有同樣的問題,它爲我工作,以及:)

羅希特