MKAnnotationView标注附件

我正在尝试使用自定义标注视图实现自定义注释。 我已经成功地在我的应用程序中进行了整合,如下所示:

在这里输入图像描述

我在xib文件中创建了我的自定义标注视图:

在这里输入图像描述

但是,当用户缩放/拖动地图时,我的标注视图停留在屏幕上的同一位置,而地图和注释处于新的位置:

在这里输入图像描述

我想知道如何使标注视图保持在注释上方,即使地图被移动,旋转或拖动。 我的标注视图的代码可以在下面找到:

- (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];
        }
    }
}

请让我知道是否有办法将标注视图固定在注释上方。 我曾尝试过使用

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

而不是使用view.superview,但是如果我尝试这样,标注视图不会显示(隐藏)。


下面我找到了一个临时解决方案 问题在于我的标注视图上的按钮不再起作用。 当我选择一个按钮时,标注消失。

- (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];
        }
    }
}

我建议你使用这个库,因为我最近用它来做我的项目。

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

它会帮助你处理mapView滚动和平移。

然后我相信你可以在SMCalloutView中加载和放置你的customView和addSubView。

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

上一篇: MKAnnotationView Callout Accessory

下一篇: IOS: How to draw and calculate position of the custom callout arrow in IOS 6?