首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IPhone - MKReverseGeocoder -如何确保我得到结果

IPhone - MKReverseGeocoder -如何确保我得到结果
EN

Stack Overflow用户
提问于 2010-08-14 07:53:30
回答 1查看 4.9K关注 0票数 0

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

所以我有一个类MapPoint:

代码语言:javascript
复制
@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

我是这样实现的:

代码语言:javascript
复制
@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:

代码语言:javascript
复制
-(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返回了一个答案(placemark或error -不管它是什么)。也许等待网络响应是错误的,我应该这样做--我只是不确定网络响应何时到达,它将相应地更新MapView中的MapView。

请尽量详细说明。谢谢

EN

回答 1

Stack Overflow用户

发布于 2010-08-15 00:36:34

  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.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3482499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档