MKAnnotationView标注附件

时间:2014-07-15 18:48:28

标签: ios objective-c xcode mapkit mkannotationview

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

enter image description here

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

enter image description here

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

enter image description here

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

- (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,但如果我尝试这个,则不会显示(隐藏)标注视图。

2 个答案:

答案 0 :(得分:0)

我在下面找到了一个临时解决方案。这个问题是我的标注视图上的按钮不再有效。当我选择一个按钮时,标注会消失。

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

答案 1 :(得分:0)

我建议您使用此库,因为我最近将它用于我的项目。

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

它将帮助您处理mapView滚动和放大声像。

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