【问题标题】:cllocation and mkreversegeocodercllocation 和 mkreversegeocoder
【发布时间】:2011-02-07 21:24:43
【问题描述】:

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

应用程序仅在我第一次获得我的价值时才有效。在第二次应用程序崩溃。 我认为是因为地理编码器已启动,我认为我必须有一个且只有一个 istance 运行。 (但我真的不喜欢这个......)。我可以通过什么方式控制地理编码器的运行?

我发现我可以使用 cllocationcoordinate2d 在我的 viewdidload 中设置 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]; 有效(什么都不做),因此无需检查。

标签: iphone cllocationmanager cllocation mkreversegeocoder


【解决方案1】:

不要将反向地理编码器创建为局部变量。

查看 Apple 提供的 CurrentAddress 示例应用程序中的 MKReverseGeocoder 示例。请参阅 MapViewController.h 和 MapViewController.m。

在您的代码中遵循相同的模式。

【讨论】:

  • shure 最后我已经这样做了,现在我已经修改了 autorelease 并且不检查地理编码器是否正在运行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-20
  • 1970-01-01
相关资源
最近更新 更多