【问题标题】:IPhone - MKReverseGeocoder - how to make sure I have the result backiPhone - 反向地理编码 - 如何确保我得到结果
【发布时间】:2011-03-29 18:59:13
【问题描述】:

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

所以我有一个类 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。

请尽可能详细说明。 谢谢

【问题讨论】:

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


    【解决方案1】:
    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.

    【讨论】:

    • 这或多或少是我的想法,但后来我开始思考我在帖子中写的内容。 1. 你看,获取地标是一个网络请求,我真的要等到我把它拿回来而不显示之前的注释吗?如果稍后我会更新 title 属性,它会反映在 mapView 上吗? 2. 现在我正在为每个注释(在 MapPoint 内部)分配一个新的 MKReverseGeocoder,这是正确的方法吗?或者有一种方法可以使用相同的,只是改变它的坐标?
    • MKReverseGeocoder 在 iOS5 中已弃用。如果您现在开始开发应用程序,则应该使用 CLGeocoder - 否则,您可能需要稍后迁移它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    相关资源
    最近更新 更多