自定义标注气泡 MKMapView

发布于 2024-11-26 11:15:43 字数 378 浏览 2 评论 0原文

我想要做的是在 MKMapView 中创建自定义标注气泡,正如 http://blog.asolutions.com/2010/09/building-custom-map-annotation-callouts-part-1/,但是看起来这个原本做得很好的应用程序中存在一些错误。例如,当您将自定义标注气泡保持打开状态并滚动离开时,地图会在某个时刻滚动回打开的标注。缩放有时也会触发此错误。有人能够解决这些问题吗?抱歉创建了一个新问题(因为有几个解决自定义标注气泡),但我没有足够的代表点来评论答案。

What I am wanting to do is to create a custom callout bubble in MKMapView, just as it is explained in http://blog.asolutions.com/2010/09/building-custom-map-annotation-callouts-part-1/, but it seems that there are some bugs in that otherwise nicely done application. For example, when you leave the custom callout bubble open, and scroll away, then at some point the map scrolls back to the open callout. Also zooming sometimes triggers this bug. Has anyone been able to solve those problems? Sorry for creating a new question (as there are a couple addressing custom callout bubble), but I did not have enough rep points to comment an answer.

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

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

发布评论

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

评论(1

西瓜 2024-12-03 11:15:43

解决这个问题的方法是在 CalloutMapAnnotationView: didMoveToSuperview,
如果我们取消选择注释,则不要调用 adjustmentMapRegionIfNeeded。

因此,我修复它的方法是在我的mapViewController:didDeselectAnnotationView中,在从mapView中删除Annotation之前,我为BasicMapAnnotationView分配一个非零标记值。我选择了 159。

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {

// distinguish between the map deselecting this and the user tapping on the callout.
//   in the former case, we want to deselect as normal; latter case, show coupon detail.

if ( [view isKindOfClass:[BasicMapAnnotationView class]] ) {
    if ( ((BasicMapAnnotationView *)view).calloutMapAnnotation ) {
        if ( !((BasicMapAnnotationView *)view).preventSelectionChange) {
            ((BasicMapAnnotationView *)view).tag = 159;  // prevent adjusting map - see CalloutMapAnnotationView
            [mapView removeAnnotation: ((BasicMapAnnotationView *)view).calloutMapAnnotation];
            // if we're deselecting the currently selected one, null out self.selectedAnnotationView
            // Otherwise, the map view is deselecting this one in order to select another one
            // and the timing is such that this nulling happens first, and the newly set AV would have no value for this.
            if ( (BasicMapAnnotationView *)view == self.selectedAnnotationView ) {
                self.selectedAnnotationView=nil;
            }
            ((BasicMapAnnotationView *)view).calloutMapAnnotation = nil;
        }
    }
}
}

然后,在 CalloutMapAnnotationView 中,我检查该值,如果找到,我不会调整地图。

- (void)didMoveToSuperview {
if ( self.parentAnnotationView ) {
    if ( self.parentAnnotationView.superview ) {   
        // null superview means it's been removed from map, and adjustMap gets wrong parentOrigin based on null superview,
        // and recenters the map somewhere in Antarctica.  The Ross Ice Shelf has no coupons except for frozen penguin eggs.

        if ( self.parentAnnotationView.tag != 159 ) {// Don't adjust map region on deselect. 
                                                     //159 hardcoded in MapViewController:didDeselectAnnotationView
            [self adjustMapRegionIfNeeded];
        }
        [self animateIn];
    } 
}
}

The fix for this is in CalloutMapAnnotationView: didMoveToSuperview,
if we're de-selecting the annotation, then do not call adjustMapRegionIfNeeded.

So the way I fixed it is in my mapViewController: didDeselectAnnotationView, I assign a non-zero tag value to the BasicMapAnnotationView right before I removeAnnotation from the mapView. I chose 159.

- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view {

// distinguish between the map deselecting this and the user tapping on the callout.
//   in the former case, we want to deselect as normal; latter case, show coupon detail.

if ( [view isKindOfClass:[BasicMapAnnotationView class]] ) {
    if ( ((BasicMapAnnotationView *)view).calloutMapAnnotation ) {
        if ( !((BasicMapAnnotationView *)view).preventSelectionChange) {
            ((BasicMapAnnotationView *)view).tag = 159;  // prevent adjusting map - see CalloutMapAnnotationView
            [mapView removeAnnotation: ((BasicMapAnnotationView *)view).calloutMapAnnotation];
            // if we're deselecting the currently selected one, null out self.selectedAnnotationView
            // Otherwise, the map view is deselecting this one in order to select another one
            // and the timing is such that this nulling happens first, and the newly set AV would have no value for this.
            if ( (BasicMapAnnotationView *)view == self.selectedAnnotationView ) {
                self.selectedAnnotationView=nil;
            }
            ((BasicMapAnnotationView *)view).calloutMapAnnotation = nil;
        }
    }
}
}

Then, in CalloutMapAnnotationView, I check for this value, and if found, I don't adjust the map.

- (void)didMoveToSuperview {
if ( self.parentAnnotationView ) {
    if ( self.parentAnnotationView.superview ) {   
        // null superview means it's been removed from map, and adjustMap gets wrong parentOrigin based on null superview,
        // and recenters the map somewhere in Antarctica.  The Ross Ice Shelf has no coupons except for frozen penguin eggs.

        if ( self.parentAnnotationView.tag != 159 ) {// Don't adjust map region on deselect. 
                                                     //159 hardcoded in MapViewController:didDeselectAnnotationView
            [self adjustMapRegionIfNeeded];
        }
        [self animateIn];
    } 
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文