Misha
Misha

Reputation: 5380

Wait for MKReverseGeocoder to provide address

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.

Upvotes: 0

Views: 943

Answers (1)

Daniel
Daniel

Reputation: 22405

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

Upvotes: 4

Related Questions