0

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.

Misha
  • 5,260
  • 6
  • 35
  • 63

1 Answers1

4

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
    }
}
Daniel
  • 22,363
  • 9
  • 64
  • 71
  • Not really. I think I did exactly what you suggested and the method didReceivePlacemark is being called. I can implement it as I want, but the problem is that I want to wait somehow for this event to occur and not to continue code. – Misha Mar 05 '11 at 19:05
  • Once again, u are going about it the wrong way, u dont execute code until thedelegate method is called, so whatever u are doing when the getplacemark method returns , do in the protocol method... – Daniel Mar 05 '11 at 22:25
  • @Daniel, I am trying to do the same thing and my didRecievePlacemark method is also getting called, but when i put a NSLog statement to see what is coming in placemark object all i am getting is empty string. – Yogesh Mar 07 '11 at 02:44
  • didReceivePlacemark will always be called! You just call back the delegate method inside of didReceivePlacemark, ill post a more detailed example soon – Daniel Mar 07 '11 at 16:58
  • It took some time to understand what you suggested. Thanks. – Misha Mar 08 '11 at 12:44
  • sorry, should have posted the example earlier – Daniel Mar 08 '11 at 16:58