使用CLGeocoder将城市和州信息添加到MKAnnotation

时间:2011-12-23 01:49:12

标签: iphone objective-c 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),(null),而位置2打印加利福尼亚州旧金山。为什么在我可以在我的注释中使用它们之前从这些属性中删除数据?

非常感谢...

1 个答案:

答案 0 :(得分:1)

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

<强>更新

您需要在completionHandler块中设置注释。块中出现的代码在序列和时间上晚于locationManager:didUpdateToLocation:fromLocation:消息实现的其余部分发生。您可能希望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);
}

您不需要额外的retain语句,因为该属性会复制该值。您还必须确保致电self.cityself.state以获取值。