Customized callout bubble MKMapView

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.


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];
    } 
}
}
链接地址: http://www.djcxy.com/p/67276.html

上一篇: 点击自定义标注后MKMapKit regionDidChangeAnimated停止

下一篇: 自定义标注泡泡MKMapView