2010-04-14 77 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