等待 MKReverseGeocoder 提供地址

发布于 2024-10-20 02:49:38 字数 628 浏览 0 评论 0原文

有没有办法等待地理编码器调用 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() 为什么调用以下方法之一,但它不起作用。

Is there a way to wait for geocoder to invoke didFailWithError or didFindPlaceMark?

My problem is that i have to call a method that receives coordinate and returns placemark holding the address. But when i call [myGeocoder start] code continues and i get an empty placemark.

My code is:

- (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;    
}

I tryed to perform sleep() whyle one of the following methods invoked, but it didn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心舞飞扬 2024-10-27 02:49:38

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

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

@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 的委托属性,并实现协议、实现可能看起来像

-(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
    }
}

I think you are going about it the wrong way, there is no reason to block, what you have to do is have that method return void, and in the class that is handling the geocoding, define a protocol that has a method say -(void)didReceivePlacemark:(id)placemark, placemark can be nil or some placemark, and it is called when the geocoder returns. You also make a delegate property for your class so anyone can subscribe to the protocol... Then in the calling class subscribe to the protocol and implement the method...heres a bit more on protocols

Hope that helps
Here is an example:
So the interface of your class that does the geocoding will look something like this

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


@end

@interface GeocoderController : NSObject {

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

Then in the implementation you would see something like this

- (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]  
}

In the calling class, all you have to set is the delegate property of the GeocoderClass, and implement the protocol, the implementation might look somethign like

-(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
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文