打开时的 MapView 注释标注操作

发布于 2024-08-28 04:28:25 字数 247 浏览 3 评论 0原文

我有一个带有几个注释的地图视图。每个注释都有一个 leftCalloutAccessoryView,它是一个 UIViewController 类。这样做的原因是我希望每个注释从服务器加载一些数据,并将该数据的结果添加到注释 subTitle 中。 这一切都很完美,除了我不想在应用程序启动时加载所有数据,但我希望仅在标注气泡打开时才完成远程调用。

有人知道我该怎么做吗? viewWillload、viewDidLoad 等。在这种情况下不起作用。还有例子吗?

I have a mapview with several annotations. Every annotation has a leftCalloutAccessoryView which is a UIViewController class. The reason for this is that I want every annotation to load some data from the server, and add the result of that data to the annotation subTitle.
This all works perfectly, except that I don't want to load all that data when my app is started, but I want to the remote call to be done only when the callout bubble is opened.

Does anybody know how I can do this? The viewWillload, viewDidLoad ect. don't work in this case. Any examples as well?

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

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

发布评论

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

评论(1

晒暮凉 2024-09-04 04:28:25

我解决了添加观察者的问题。然后观察者做它的事情,然后标注就会出现。

我遇到的问题是,在显示气泡后,我无法更新标注气泡中的信息。做到这一点的唯一方法是创建自己的标注气泡(据我所知),考虑到我有最后期限,我不喜欢这样做。
我通过添加一个额外的 UIView 来解决这个问题,上面有一个 alpha 和一个文本“获取位置数据...”。我只是在按下某个位置时显示该视图,当观察者完成时,我再次隐藏该视图(当然是使用动画)。

希望我的回答对其他人有帮助。

代码:

[pin addObserver:self
      forKeyPath:@"selected"
         options:NSKeyValueObservingOptionNew
         context:GMAP_ANNOTATION_SELECTED];

更多代码:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context{

    NSString *action = (NSString*)context;

    MKAnnotationView *annotationView = [(MKAnnotationView*)object retain];
    BikeAnnotation *bike = [[annotationView annotation] retain];

    if([action isEqualToString:GMAP_ANNOTATION_SELECTED] && [[bike _stationType] intValue] != 5 && [[bike _stationType] intValue] != 6){
        BOOL annotationAppeared = [[change valueForKey:@"new"] boolValue];
        if (annotationAppeared) {
            NSLog(@"Annotation selected");
        else {
            NSLog(@"annotation deselected");
        }
    }
}

并将其放在 @synthesize 之后:

NSString * const GMAP_ANNOTATION_SELECTED = @"gmapselected";

I solved the issue adding an observer. The observer then does its thing and after that the callout shows up.

Something I had problems with is that I couldn't update the information in the callout bubble after the bubble is shown. The only way to do this is to create your own callout bubble (as I understand it), which is something I didn't feel like given that I have a deadline.
I fixed that by adding an extra UIView with an alpha on it and a text "Getting location data...". I just show up that view when pressing an location and when the observer is done, I hide the view again (off course by using an animation).

Hope my answer helped others.

Code:

[pin addObserver:self
      forKeyPath:@"selected"
         options:NSKeyValueObservingOptionNew
         context:GMAP_ANNOTATION_SELECTED];

Some more code:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context{

    NSString *action = (NSString*)context;

    MKAnnotationView *annotationView = [(MKAnnotationView*)object retain];
    BikeAnnotation *bike = [[annotationView annotation] retain];

    if([action isEqualToString:GMAP_ANNOTATION_SELECTED] && [[bike _stationType] intValue] != 5 && [[bike _stationType] intValue] != 6){
        BOOL annotationAppeared = [[change valueForKey:@"new"] boolValue];
        if (annotationAppeared) {
            NSLog(@"Annotation selected");
        else {
            NSLog(@"annotation deselected");
        }
    }
}

And put this just after the @synthesize's:

NSString * const GMAP_ANNOTATION_SELECTED = @"gmapselected";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文