IPhone - MKReverseGeocoder - 如何确保我有结果

时间:2010-08-14 07:53:31

标签: iphone objective-c cocoa-touch mkmapview mkreversegeocoder

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

所以我有一个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];

}

现在,我有两个我不确定的问题:

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

  2. 我怎样才能确定当我的注释显示时我肯定知道reverseGeo回来了一个答案(地标或错误 - 无论它是什么)。 也许等待网络响应是错误的,我应该这样做 - 我只是不确定当/如果网络响应到达它会相应地更新MapView中的annotationView。

  3. 请尽可能详细说明。 感谢

1 个答案:

答案 0 :(得分:0)

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

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

  3. 顺便说一句,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.