0

我想要达到的是显示带有城市名称的注释。

所以我有一个类 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 个问题我不确定:

  1. 将 reverseGeo 作为数据成员是否正确,或者更好的选择是将其分配在初始化程序中并在 didFindPlacemark/didFailWithError 委托中释放它(甚至可以在那里释放它)?

  2. 我怎样才能确保当我的注释显示时我确定 reverseGeo 会返回一个答案(地标或错误 - 不管它是什么)。也许等待网络响应是错误的,我应该就这样离开它——我只是不确定网络响应何时/是否会到达,它将相应地更新 MapView 中的 annotationView。

请尽可能详细说明。谢谢

4

1 回答 1

0
  1. 可以将其存储为数据成员。

  2. 看起来您要为用户的当前位置留下注释?如果您要使用“面包屑轨迹”来补充常规用户的当前位置注释,以显示用户所在的位置,那么您需要等待将点添加到地图,直到您获得注释(如果这是行为你要)。我要么通过使管理您的地图的类成为 MKReverseGeocoder 委托(并让它设置标题属性,然后将注释添加到地图中reverseGeocoder:didFindPlacemark)来做到这一点,要么将地图引用添加到您的 MapPoint 类并让它自己添加到同一个回调中的地图。

顺便说一句,MKReverseGeocoder 的文档包括以下文本:

  • When you want to update the location automatically (such as when the user is moving), reissue the reverse-geocoding request only when the user's location has moved a significant distance and after a reasonable amount of time has passed. For example, in a typical situation, you should not send more than one reverse-geocode request per minute.
于 2010-08-15T00:36:34.033 回答