2011-10-15 4 views
2

J'essaie d'inverser le géocodage d'un emplacement, j'ai réussi à le faire mais j'ai rencontré un léger problème qui m'empêche de le terminer. Je pense que je sais ce qu'il fait et pourquoi, mais je ne suis pas en mesure de trouver un moyen de contourner encore. (J'utilise ARC et iOS5)Gestion de l'événement Async dans CLGeocoder (iOS5)

Le problème se produit lorsque j'exécute le code suivant dans ma méthode init ...

[self setSubtitle: [dateString stringByAppendingString: self.city ] ]; 

self.city revient en arrière une valeur nulle, je suis assez sûr que ce est parce que la méthode init complète WAY avant que le géocodeur puisse aller faire sa chose.

Donc, ma question est, comment puis-je gérer cette situation? Je suppose que je suis assis et j'attends que le géocodeur termine son travail et gèle l'application jusqu'à ce que ce ne soit pas le chemin à parcourir?

#import "MapPoint.h" 

@implementation MapPoint 
@synthesize coordinate, title, dateAdded, subtitle, city; 

-(id)initWithCoordinates:(CLLocationCoordinate2D)c 
        title:(NSString *)t { 

    self = [super init]; 
    if (self) { 

     coordinate = c; 
     [self setTitle: t]; 
     [self setCurrentCity: [[CLLocation alloc] initWithLatitude:c.latitude longitude:c.longitude]]; 

     NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
     [formatter setTimeStyle:NSDateFormatterNoStyle]; 
     [formatter setDateStyle:NSDateFormatterLongStyle]; 

     [self setDateAdded: [[NSDate alloc] init]]; 
     NSString *dateString = [formatter stringFromDate: [self dateAdded]]; 

     [self setSubtitle: [dateString stringByAppendingString: self.city ] ]; 

    } 
    return self; 

} 

-(void)setCurrentCity: (CLLocation *)loc { 

    CLGeocoder *reverseGeo; 

    if (!reverseGeo) { 
     reverseGeo = [[CLGeocoder alloc] init]; 
    } 

    [reverseGeo reverseGeocodeLocation: loc completionHandler: 
    ^(NSArray *placemarks, NSError *error) { 
     if ([placemarks count] > 0) { 

      CLPlacemark *topResult = [placemarks objectAtIndex:0]; 
      self.city = [NSString stringWithFormat:@"%@", [topResult locality]]; 

     } 
    }]; 
} 

@end 

Répondre

2

s'avère que je devais utiliser une boucle pour parcourir les résultats et définir le sous-titre d'annotation d'ici.

#import "MapPoint.h" 

@implementation MapPoint 
@synthesize coordinate, title, dateAdded, subtitle, city, reverseGeo; 

-(id)initWithCoordinates:(CLLocationCoordinate2D)c 
        title:(NSString *)t { 

    self = [super init]; 
    if (self) { 

     coordinate = c; 
     [self setTitle: t]; 
     [self setCurrentCity: [[CLLocation alloc] initWithLatitude:c.latitude longitude:c.longitude]]; 

     [self setDateAdded: [[NSDate alloc] init]]; 

    } 
    return self; 

} 

-(void)setCurrentCity: (CLLocation *)loc { 

    if (!self.reverseGeo) { 
     self.reverseGeo = [[CLGeocoder alloc] init]; 
    } 

    [self.reverseGeo reverseGeocodeLocation: loc completionHandler: 
    ^(NSArray *placemarks, NSError *error) { 

     for (CLPlacemark *placemark in placemarks) { 

      NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
      [formatter setTimeStyle:NSDateFormatterNoStyle]; 
      [formatter setDateStyle:NSDateFormatterLongStyle]; 

      NSString *dateString = [formatter stringFromDate: [self dateAdded]]; 
      [self setSubtitle: [dateString stringByAppendingString: [placemark locality] ] ];    

     } 
    }]; 
} 

@end 
-1

vous devez faire quelque chose comme cela dans la méthode reverseGeocoder didFindPlacemark:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark { 


    //progressLabel.text = NSLocalizedString(@"Location Determined", @"Location Determined"); 



    MapLocation *annotation = [[[MapLocation alloc] init] autorelease]; 
    annotation.streetNumber = placemark.subThoroughfare; 
    annotation.streetAddress = placemark.thoroughfare; 
    annotation.city = placemark.locality; 
    } 
+0

Je ne suis pas en utilisant MKReverseGeocoder car il est maintenant dépréciée avec iOS5, j'utilise la nouvelle classe CLGeocoder qui semble avoir aucune forme de protocole de délégation :( – bagwaa