【发布时间】:2011-12-23 01:49:11
【问题描述】:
我正在尝试将城市和州信息添加到 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 打印加利福尼亚州旧金山。为什么在我可以在注释中使用它们之前从这些属性中删除数据?
非常感谢...
【问题讨论】:
-
你在哪里设置 MKAnnotation?
-
我添加了一些代码来显示我在哪里设置 MKAnnotation。 MapPoint 类符合 MKAnnotation 协议。不要担心日期的东西。如果我在做注释之前能得到城市和州,我会把它添加到副标题中。
-
这就解释了问题所在。请参阅我的答案中的更新部分。
标签: iphone objective-c mkannotation clgeocoder