我试图将城市&状态信息添加到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),而位置2打印加利福尼亚州旧金山。为什么数据在我可以在我的注释中使用之前从这些属性中删除?
许多感谢...
你在哪里设置MKAnnotation? – 2011-12-23 02:19:49
我添加了一些代码来显示我设置MKAnnotation的位置。类MapPoint符合MKAnnotation协议。不要担心日期的事情。如果我在做出注释之前能够获得城市和州,我会将其添加到副标题中。 – 2011-12-23 03:05:53
这解释了这个问题。请参阅我答案中的更新部分。 – 2011-12-23 03:15:52