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



我想添加城市&状态信息到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

Location 1打印(null), (null),而Location 2打印San Francisco, California。为什么在我可以在注释中使用这些属性之前要从这些属性中删除数据?

谢谢…

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

需要在completionHandler块中设置注释。在块中发生的代码在顺序和时间上比locationManager:didUpdateToLocation:fromLocation:消息实现的其余部分晚。你可能希望从苹果的文档中阅读有关block的内容,或者在互联网上搜索。

- (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来获取这些值。

相关内容

最新更新