我想要达到的是显示带有城市名称的注释。
所以我有一个类 MapPoint :
@interface MapPoint : NSObject<MKAnnotation,MKReverseGeocoderDelegate> {
NSString* title;
NSString* cityName;
CLLocationCoordinate2D coordinate;
MKReverseGeocoder* reverseGeo;
}
@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString* title;
@property (nonatomic,copy) NSString* cityName;
-(id) initWithCoordinate:(CLLocationCoordinate2D)c tilte:(NSString*)t;
@end
我是这样实现的:
@implementation MapPoint
@synthesize title,coordinate,cityName;
-(id) initWithCoordinate:(CLLocationCoordinate2D)c tilte:(NSString*)t
{
[super init];
coordinate = c;
reverseGeo = [[MKReverseGeocoder alloc] initWithCoordinate:c];
reverseGeo.delegate = self;
[reverseGeo start];
[self setTitle:t];
return self;
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
NSString* city = [placemark.addressDictionary objectForKey:(NSString*)kABPersonAddressCityKey];
NSString* newString = [NSString stringWithFormat:@"city-> %@",city];
[self setTitle:[title stringByAppendingString:newString]];
}
-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{
NSLog(@"error fetching the placemark");
}
-(void)dealloc
{
[reverseGeo release];
[cityName release];
[title release];
[super dealloc];
}
@end
然后,在我的 CoreLocation 委托中,我像这样使用 MapPoint:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
MapPoint* mp = [[MapPoint alloc] initWithCoordinate:[newLocation coordinate] tilte:[locationTitleField text]];
[mapView addAnnotation:mp];
[mp release];
}
现在,我有 2 个问题我不确定:
将 reverseGeo 作为数据成员是否正确,或者更好的选择是将其分配在初始化程序中并在 didFindPlacemark/didFailWithError 委托中释放它(甚至可以在那里释放它)?
我怎样才能确保当我的注释显示时我确定 reverseGeo 会返回一个答案(地标或错误 - 不管它是什么)。也许等待网络响应是错误的,我应该就这样离开它——我只是不确定网络响应何时/是否会到达,它将相应地更新 MapView 中的 annotationView。
请尽可能详细说明。谢谢