iphone - cllocation 和 mkreversegeocoder

标签 iphone cllocationmanager cllocation mkreversegeocoder

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

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

我发现我可以使用 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

最佳答案

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

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

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

关于iphone - cllocation 和 mkreversegeocoder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2637152/

相关文章:

iphone - 在触摸事件中如何获取所有激活的像素?

ios - SwiftUI - 如何模糊 View 的默认背景颜色?

ios - 使用 requestLocation 仅请求一次用户位置

iphone - CLLocation 和 double 的问题

iOS 存储 CLLocation 核心数据作为可转换的 NSKeyedUnarchiveFromDataTransformerName 错误 CoreData : warning: no NSValueTransformer

html - 密码输入字段在 ios safari 中不起作用

iphone - iPhone 应用程序中的 I18n 是否需要所有这些步骤?

iOS 模拟器崩溃 requestAlwaysAuthorization()

ios - 在加载 WKWebView 之前尝试获取 CLLocation 坐标

swift - 如何让Swift持续更新速度