gpt4 book ai didi

iphone - 等待 MKReverseGeocoder 提供地址

转载 作者:行者123 更新时间:2023-12-03 17:32:32 25 4
gpt4 key购买 nike

有没有办法等待地理编码器调用 didFailWithError 或 didFindPlaceMark?

我的问题是我必须调用一个接收坐标并返回保存地址的地标的方法。但是当我调用 [myGeocoder start] 代码继续时,我得到一个空地标。

我的代码是:

- (MKPlasemark*) getAddress:(CLLocationCoordinate2D) coordinate
{
[self startGeocoder:coordinate];
return self.foundPlasemark;
}

- (void)reverseGeocoder:(MKReverseGeocoder*)geocoder didFindPlacemark:(MKPlaseMark*)plasemark
{
self.foundPlasemark=plasemark;
}

- (void)reverseGeocoder:(MKReverseGeocoder*)geocoder didFailWithError:(NSError*)error
{
self.foundPlasemark=nil;
}

我尝试执行 sleep() 为什么调用以下方法之一,但它不起作用。

最佳答案

我认为你的处理方式是错误的,没有理由阻止,你所要做的就是让该方法返回 void,并在处理地理编码的类中,定义一个具有方法的协议(protocol)比如说 -(void)didReceivePlacemark:(id)placemark,placemark 可以是 nil 或某个地标,并且在地理编码器返回时调用它。您还可以为您的类创建一个委托(delegate)属性,以便任何人都可以订阅该协议(protocol)...然后在调用类中订阅该协议(protocol)并实现该方法...这里有更多关于 protocols 的信息。

希望有帮助这是一个例子:因此,执行地理编码的类的接口(interface)将如下所示

@protocol GeocoderControllerDelegate
-(void)didFindGeoTag:(id)sender; // this is the call back method


@end

@interface GeocoderController : NSObject {

id delegate;
}
@property(assign) id <GeocoderControllerDelegate> delegate;

然后在实现中你会看到类似这样的内容

- (void) getAddress:(CLLocationCoordinate2D) coordinate
{
[self startGeocoder:coordinate];
}

- (void)reverseGeocoder:(MKReverseGeocoder*)geocoder didFindPlacemark:(MKPlaseMark*)plasemark
{
[delegate didFindGeoTag:plasemark];
}

- (void)reverseGeocoder:(MKReverseGeocoder*)geocoder didFailWithError:(NSError*)error
{
[delegate didFindGeoTag:nil]
}

在调用类中,您只需设置 GeocoderClass 的委托(delegate)属性,并实现协议(protocol),实现可能看起来像这样

-(void)findMethod
{

GeocoderController *c=...
[c setDelegate:self];
[c findAddress];
//at this point u stop doing anything and just wait for the call back to occur
//this is much preferable than blocking
}

-(void)didFindGeoTag:(id)sender
{
if(sender)
{
//do something with placemark
}
else
{
//geocoding failed
}
}

关于iphone - 等待 MKReverseGeocoder 提供地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5197586/

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