我試圖將城市&狀態信息添加到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