自定义标注气泡MKMapView

时间:2011-07-24 15:34:55

标签: iphone mkmapview

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

1 个答案:

答案 0 :(得分:-1)

对此的修复是在CalloutMapAnnotationView:didMoveToSuperview中,    如果我们取消选择注释,则不要调用adjustMapRegionIfNeeded。

所以我修复它的方法是在我的mapViewController:didDeselectAnnotationView中,在从mapView中删除注释之前,我将一个非零标记值分配给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];
    } 
}
}