2010-04-14 78 views
3

我嘗試使用cllocation和mkreversegeocoder撤回城市名稱。 在我viewDidLoad方法我istance cllocationmanager:cllocation和mkreversegeocoder

self.locManager = [[CLLocationManager alloc] init]; 
locManager.delegate = self; 
locManager.desiredAccuracy = kCLLocationAccuracyBest; 
[locManager startUpdatingLocation]; 

後:

- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation 
*)newLocation 
fromLocation:(CLLocation *)oldLocation { 
//some code to retrieve my information 

MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc]  
initWithCoordinate:newLocation.coordinate ]; 
geoCoder.delegate = self; 
[geoCoder start]; 
} 

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark 
*)placemark 
{ 
MKPlacemark *myPlacemark = placemark; 
citta.text = [placemark locality]; 
} 

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{ 
citta.text [email protected] "Unknow"; 

應用程序僅在第一時間讓我的價值。在第二次應用程序崩潰。 我認爲是因爲地理編碼器已啓動,我認爲我必須有一個且只有一個運行。 (但我真的不知道這個......)。在哪種方式我可以控制地理編碼器運行?

我已經看到,我可以在我的viewdidload中使用cllocationcoordinate2d istance mkreversegeocoder但是...在哪種方式我可以retreive newlocation.coordinate?

我已經部分地解決地址解析器的聲明作爲類變量和檢查

if (self.geocoder != nil) { 
    [geocoder release]; 
} 

,但....如果我

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark 
*)placemark 

或在我

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailedWithError:(@NSError 
*)error 

我釋放或取消我的對象? 我感覺很愚蠢:D

+0

'[無釋放];'有效(無所作爲),所以不需要檢查。 – 2011-05-31 17:01:25

回答

7

不要將反向地理編碼器創建爲局部變量。

看看Apple提供的CurrentAddress示例應用程序中的MKReverseGeocoder示例。請參閱MapViewController.h和MapViewController.m。

遵循您的代碼中的相同模式。

+0

舒爾在最後我已經這樣做了,現在我已經修改autorelease一個不檢查,如果geocoder反正在運行 – zebra 2010-04-14 14:24:54