我正在尝试使用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