2011-12-23 64 views
0

我试图将城市&状态信息添加到MKAnnotation,但是在将数据添加到注释之前数据正在清除。这里是我当前的代码(只显示为简单起见关注的章节/部分):使用CLGeocoder向MKAnnotation添加城市和州信息

实施:

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{  
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; 
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
    CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
    [self setCity:[placemark locality] andState:[placemark administrativeArea]]; 
    }]; 

    NSLog(@"Location 1: %@, %@", city, state); 

    MapPoint *mp = [[MapPoint alloc] initWithCoordinate:[newLocation coordinate] 
                title:[locationTitleField text] 
                date:[dateFormat stringFromDate:today]]; 

    [mapView addAnnotation:mp]; 
} 

- (void)setCity:(NSString *)c andState:(NSString *)s 
{ 
    [self setCity:c]; 
    [city retain]; 
    [self setState:s]; 
    [state retain]; 
    NSLog(@"Location 2: %@, %@", city, state); 
} 

接口:

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate, MKMapViewDelegate> 
{ 
    CLLocationManager *locationManager; 
    IBOutlet MKMapView *mapView; 
    NSString *city; 
    NSString *state; 
} 

@property (strong, nonatomic) IBOutlet UIWindow *window; 
@property (nonatomic, copy) NSString *city; 
@property (nonatomic, copy) NSString *state; 

- (void)setCity:(NSString *)c andState:(NSString *)s; 

@end 

位置1张打印(空)( null),而位置2打印加利福尼亚州旧金山。为什么数据在我可以在我的注释中使用之前从这些属性中删除?

许多感谢...

+0

你在哪里设置MKAnnotation? – 2011-12-23 02:19:49

+0

我添加了一些代码来显示我设置MKAnnotation的位置。类MapPoint符合MKAnnotation协议。不要担心日期的事情。如果我在做出注释之前能够获得城市和州,我会将其添加到副标题中。 – 2011-12-23 03:05:53

+0

这解释了这个问题。请参阅我答案中的更新部分。 – 2011-12-23 03:15:52

回答

1

原因位置1是空的是,在运行时的反向地理编码尚未完成。在地理编码器完成后,您的completionHandler中的所有内容都将发生。您的位置1的日志语句不是该completionHandler的一部分,并在调用反向地理编码之后立即执行。最终的结果是日志调用发生在之前,您请拨打setCity:andState:

更新

您需要设置注释在completionHandler块。代码块中出现的代码的发生时间顺序和时间要晚于您的其他消息实现。您可能希望read about blocks from Apple's documentation或搜索互联网。

- (void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
      fromLocation:(CLLocation *)oldLocation 
{  
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; 
    [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
     CLPlacemark *placemark = [placemarks objectAtIndex:0]; 
     [self setCity:[placemark locality] andState:[placemark administrativeArea]]; 
     MapPoint *mp = [[MapPoint alloc] initWithCoordinate:[newLocation coordinate] 
                 title:[locationTitleField text] 
                 date:[dateFormat stringFromDate:today]]; 
     [mapView addAnnotation:mp]; 
    }]; 
} 

末更新

此外,它看起来就好像你可能没有使用性能,以及你能。由于您有citystate属性,因此您不需要citystate实例变量。

尝试改变它是这样:

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate, MKMapViewDelegate> 
{ 
    CLLocationManager *locationManager; 
    IBOutlet MKMapView *mapView; 
} 

在实施:

@synthesize city, state 
... 
- (void)setCity:(NSString *)c andState:(NSString *)s 
{ 
    [self setCity:c]; 
    [self setState:s]; 
    NSLog(@"Location 2: %@, %@", city, state); 
} 

你不需要因为属性的副本额外保留报表的价值。您还必须确保拨打self.cityself.state以获取值。

+0

这些额外的保留是希望保持数据不被清除。那么,如何获取城市和州信息以便在我的MKAnnotation中使用?好像completionHandler块是代码中运行的最后一件事,不管我如何构造它。 – 2011-12-23 03:00:47

+0

查看我更新的答案。 – 2011-12-23 03:16:40

+0

谢谢,大卫。我最终没有打扰城市和州属性。我只是通过以下方式将信息发送给MapPoint:[NSString stringWithFormat:@“%@,%@”,[placemark locality],[placemark administrativeArea]]; – 2011-12-23 17:05:22