cllocation and mkreversegeocoder

3

我正在尝试使用CLLocation和MKReverseGeocoder获取城市名称。 在我的viewDidLoad方法中,我实例化了CLLocationManager:

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 =@ "Unknow";

应用程序仅在第一次获取值时运行。第二次应用程序崩溃。我认为这是因为地理编码器已经启动,而我认为我必须只运行一个实例(但我真的不确定...)。我如何控制地理编码器正在运行?

我发现我可以在viewdidload中使用cllocationcoordinate2d实例化mkreversegeocoder,但是...我如何检索newlocation.coordinate?

我部分解决了声明地理编码器作为类变量并进行检查。

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

但是……如果在我的

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

或者在我的
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailedWithError:(@NSError 
*)error

我应该释放还是取消我的对象? 感觉自己好傻 :D


[nil release]; 可以工作(不会有任何操作),因此不需要检查。 - user142019
1个回答

7
不要将反向地理编码器创建为局部变量。
查看Apple提供的CurrentAddress示例应用程序中的MKReverseGeocoder示例。请参见MapViewController.h和MapViewController.m。
在您的代码中遵循相同的模式。

最后我已经这样做了,现在我已经使用autorelease进行了修改,无论geocoder是否正在运行都没有进行检查。 - zebra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接