2011-03-04 52 views
0

有沒有辦法等待geocoder調用didFailWithError或didFindPlaceMark?等待MKReverseGeocoder提供地址

我的問題是,我不得不調用一個方法,接收座標並返回地址的地址。但是當我調用[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;  
} 

我tryed執行睡眠()調用whyle下面的方法之一,但沒有奏效。

回答

4

我認爲你是錯誤的方式,沒有理由阻止,你必須做的是讓該方法返回void,並且在處理地理編碼的類中,定義一個協議方法說 - (void)didReceivePlacemark:(id)地標,地標可以是零或某個地標,並且在地理編碼返回時調用。您還爲您的類的委託屬性,因此任何人都可以訂閱協議...然後在調用類訂閱協議和實現方法...繼承人多一點上protocols

希望幫助 這裏有一個例子: 所以你的類,它的地理編碼會是這個樣子

@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的委託財產,並執行該協議,實現可能看起來像somethign

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

不是真的。我想我完全按照你的建議和didReceivePlacemark被調用的方法。我可以根據需要實現它,但問題是我想等待某種情況發生此事件,而不是繼續執行代碼。 – Misha 2011-03-05 19:05:34

+0

再一次,你是錯誤的方式,你不會執行代碼,直到調用delegate方法,所以無論你在getplacemark方法返回時做什麼,在協議方法中做... – Daniel 2011-03-05 22:25:00

+0

@Daniel,我試圖做同樣的事情,我didRecievePlacemark方法也被調用,但是當我把一個NSLog語句來看看什麼是在地標對象中,我所得到的是空字符串。 – Yogesh 2011-03-07 02:44:15