0

我正在尝试将城市和州信息添加到 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), (null) 而位置 2 打印加利福尼亚州旧金山。为什么在我可以在注释中使用它们之前从这些属性中删除数据?

非常感谢...

4

1 回答 1

1

位置 1 为空的原因是反向地理编码器在运行时尚未完成。地理编码器完成,您的一切completionHandler都会发生。您对位置 1 的日志语句不是其中的一部分,而是在调用反向地理编码后立即执行。最终结果是日志调用发生您调用之前。completionHandlersetCity:andState:

更新

您需要在completionHandler块中设置注释。块中出现的代码在顺序和时间上比locationManager:didUpdateToLocation:fromLocation:消息实现的其余部分出现得晚。您可能希望阅读 Apple 文档中的有关块或搜索互联网。

- (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];
    }];
}

结束更新

此外,看起来您可能没有尽可能地使用属性。由于您有cityandstate属性,因此您不需要cityandstate实例变量。

尝试将其更改为:

@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获取值。

于 2011-12-23T02:26:44.163 回答