简体   繁体   English

使用CLGeocoder将城市和州信息添加到MKA注释中

[英]Adding City and State Information to MKAnnotation Using CLGeocoder

I'm trying to add city & state information to an MKAnnotation but the data is being cleared before I can add it to the annotation. 我正在尝试将城市和州信息添加到MKAnnotation中,但是在将数据添加到注释中之前已清除了数据。 Here is my current code (only showing the sections/parts of concern for simplicity): 这是我当前的代码(为简单起见,仅显示需要关注的部分):

in implementation: 在实施中:

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

in interface: 在界面中:

@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 prints (null), (null) while Location 2 prints San Francisco, California. 位置1打印(null),(null),而位置2打印加州旧金山。 Why is the data being erased from these properties before I can use them in my annotation? 为什么在我可以在批注中使用它们之前从这些属性中删除数据?

Much thanks... 非常感谢...

The reason Location 1 is null is that the reverse geocoder has not completed when it is run. 位置1为空的原因是,反向地理编码器在运行时尚未完成。 Everything in your completionHandler will occur after the geocoder finishes. 在你的一切completionHandler地址解析器结束会发生。 Your log statement for location 1 is not part of that completionHandler and is executed immediately after the call to reverse geocode. 您在位置1的日志语句不是该completionHandler一部分,而是在调用反向地理编码后立即执行。 The end result is that the log call occurs before you call setCity:andState: . 最终结果是调用setCity:andState: 之前发生了日志调用。

Update 更新资料

You need to set the annotation in the completionHandler block. 您需要在completionHandler块中设置注释。 The code which occurs in the block occurs later in sequence and time than the the rest of your locationManager:didUpdateToLocation:fromLocation: message implementation. 出现在块中的代码在顺序和时间上晚于locationManager:didUpdateToLocation:fromLocation:消息实现的其余部分。 You may wish to read about blocks from Apple's documentation or search the internet. 您可能希望阅读有关Apple文档中的内容或搜索Internet。

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

End Update 结束更新

Also, it looks as if you may not be using properties as well as you could. 另外,看起来您好像并没有很好地使用属性。 Since you have a city and state property, you do not need the city and state instance variables. 由于您具有citystate属性,因此不需要citystate实例变量。

Try to change it as such: 尝试这样更改它:

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

In implementation: 在实施中:

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

You will not need the extra retain statements because the property copies the value. 您不需要额外的keep语句,因为该属性复制了值。 You must also be sure to call self.city and self.state to get the values. 您还必须确保调用self.cityself.state来获取值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM