gpt4 book ai didi

iphone - cllocation 和 mkreversegeocoder

转载 作者:行者123 更新时间:2023-12-03 19:35:35 28 4
gpt4 key购买 nike

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

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com