MKAnnotationView Callout Accessory

I am trying to implement a custom annotation with a custom callout view. I have successfully integrated both within my app, as you can see below:

在这里输入图像描述

I have created my custom callout view in a xib file:

在这里输入图像描述

However, when the user zooms/drags the map, my callout view stays in the same spot on the screen while the map and annotation is in a new position:

在这里输入图像描述

I am wondering how I can make the callout view stay above the annotation even when the map is moved, rotated or dragged. The code for my callout view can be found below:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;  //return nil to use default white dot view
    }

    if ([annotation isKindOfClass:[FishAnnotation class]])
    {
        static NSString *reuseId = @"seafoodRestaurant";
        MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];

        if (annotationView == nil)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
        }
        else
        {
            annotationView.annotation = annotation;
        }

        FishAnnotation *ann = (FishAnnotation *)annotation;
        annotationView.image = [UIImage imageNamed:ann.imageName];
        annotationView.draggable = YES;
        annotationView.canShowCallout = NO;

        return annotationView;
    }

    //return nil (default view) if annotation is not our custom type
    return nil;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{

        CalloutView *calloutView = (CalloutView *)[[[NSBundle mainBundle] loadNibNamed:@"calloutView" owner:self options:nil] objectAtIndex:0];

        CGRect calloutViewFrame = calloutView.frame;

        calloutViewFrame.origin = CGPointMake(view.frame.origin.x - calloutView.frame.size.width/2 + 18, view.frame.origin.y-calloutView.frame.size.height);
        calloutView.frame = calloutViewFrame;
        FishAnnotation *annotation = view.annotation;

        [calloutView setTitle:@"Seafood Restaurant!" subTitle:@"647-123-4567"];

        calloutView.annotation = view.annotation;

        [view.superview addSubview:calloutView];
        [view.superview bringSubviewToFront:calloutView];


}

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    for (UIView *subview in view.superview.subviews)
    {
        if ([subview isKindOfClass:[CalloutView class]])
        {
            [subview removeFromSuperview];
        }
    }
}

Please let me know if there is a way to have the callout view fixed above the annotation. I have tried to use

        [view addSubview:calloutView];
        [view bringSubviewToFront:calloutView];

instead of using view.superview, however the callout view does not display (it's hidden) if I try this.


I found a temporary solution below. The problem with this is that the buttons on my callout view no longer work. The callout disappears when I select a button.

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
    FishAnnotation *annotation = view.annotation;

    // [appleMapView setCenterCoordinate:annotation.coordinate animated:YES];

    if(![view.annotation isKindOfClass:[MKUserLocation class]])
    {
        CalloutView *calloutView = (CalloutView *)[[[NSBundle mainBundle] loadNibNamed:@"calloutView" owner:self options:nil] objectAtIndex:0];

        CGRect calloutViewFrame = calloutView.frame;

        calloutViewFrame.origin = CGPointMake(-calloutView.frame.size.width/2 + 18, -calloutView.frame.size.height);
        calloutView.frame = calloutViewFrame;

        [calloutView setTitle:annotation.title subTitle:annotation.subTitle];

        calloutView.annotation = view.annotation;

        [view addSubview:calloutView];
        [view bringSubviewToFront:calloutView];
    }
}

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    for (UIView *subview in view.subviews)
    {
        if ([subview isKindOfClass:[CalloutView class]])
        {
            [subview removeFromSuperview];
        }
    }
}

I suggest you to use this library because I've used it for my project recently.

SMCalloutView https://github.com/nfarina/calloutview

It will help you deal with the mapView scrolling & panning.

Then I believe you can load and place your customView and addSubView inside the SMCalloutView.

链接地址: http://www.djcxy.com/p/67286.html

上一篇: 颤抖没有绘制箭头很多蓝色,matlab

下一篇: MKAnnotationView标注附件