iPhone - MKReverseGeocoder - 如何确保我得到结果

发布于 2024-09-14 11:29:49 字数 2183 浏览 3 评论 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. 将verseGeo 作为数据是否正确会员,或者更好的选择就是 在初始化程序中分配它并在 didFindPlacemark/didFailWithError 委托中释放它(甚至可以在那里释放它)?

  2. 我如何确保当我的注释显示时,我确定reverseGeo返回了答案(地标或错误 - 无论它是什么)。 也许等待网络响应是错误的,我应该这样保留它 - 我只是不确定何时/如果网络响应到达,它将相应地更新 MapView 中的注释视图。

请尽可能详细说明。 谢谢

what i'm trying to reach is to display annotation with the city name.

So I have a class 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

I implemented it like this :

@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

Then, in my CoreLocation delegate I use MapPoint like that:

-(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];

}

Now, I have 2 issues I'm not sure of :

  1. Is it correct to put reverseGeo as a data member , or a better option would be to just
    alloc it inside the initializer and release it inside the didFindPlacemark/didFailWithError delegates(is it even possible to release it there) ?

  2. How can I make sure then when my annotation get displayed I know for sure that the reverseGeo came back with an answer (placemark or error - whatever it is).
    Maybe it's just wrong to wait for network response and I should leave it like that - I'm just not sure then when/if network response will arrive it will update the annotationView within the MapView accordingly.

Please elaborate as much as you can.
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤檠 2024-09-21 11:29:49
  1. 可以将其存储为数据成员。

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

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

  • 当您想要自动更新位置时(例如当用户移动时),仅当用户位置移动了相当远的距离时才重新发出反向地理编码请求经过一段合理的时间后。例如,在典型情况下,每分钟发送的反向地理编码请求不应超过一个。
  1. It's fine to store it as a data member.

  2. It looks like you're leaving a trail of annotations for the user's current location? If you're supplementing a regular user's-current-location annotation with a "bread crumb trail" showing where the user has been, then you need to wait to add the point to the map until you get the annotation back (if that's the behavior you want). I would either do that by making the class that manages your map be the MKReverseGeocoder delegate (and have it set the title property and then add the annotation to the map in reverseGeocoder:didFindPlacemark) or add a map reference to your MapPoint class and have it add itself to the map in the same callback.

By the way, the documentation for MKReverseGeocoder includes the following text:

  • 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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文