有没有办法等待地理编码器调用didFailWithError或didFindPlaceMark?
我的问题是,我必须调用一个接收坐标并返回包含地址的placemark的方法。但是当我调用myGeocoder时,开始的代码继续,我得到一个空的placemark。
我的代码是:
- (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(),但不起作用。
发布于 2011-03-05 02:48:31
我认为你做错了,没有理由阻塞,你要做的是让那个方法返回空,在处理地理编码的类中,定义一个协议,它有一个方法-(空)didReceivePlacemark:(Id) placemark,placemark可以是nil或某个placemark,当地理编码器返回时,它会被调用。您还可以为您的类创建一个委托属性,这样任何人都可以订阅该协议...然后在调用类中,订阅协议并在protocols上更多地实现method...heres
我希望这里有一个例子可以帮助我们:您的类进行地理编码的接口将如下所示
@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
}
}https://stackoverflow.com/questions/5197586
复制相似问题