使用自定义视图和图像视图的MKAnnotationView

6
在我的地图应用程序中,我想要显示一个带有图像的彩色背景圆形,而不是显示一个标记针。背景圆的颜色(在下面的图片中是绿色系的)是动态的。它将如下图所示:

enter image description here

我创建了 TCircleView,它在“drawRect”中绘制颜色。

为了显示类似的注释,我创建了 TCircleView 和 UIImageView 对象,并将它们添加到 MKAnnotationView 对象中。它看起来很好,如预期的那样可见。

但是它不允许检测点击/触摸以显示弹出窗口。

我正在使用以下代码:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

    if ([annotation isKindOfClass:[MKPointAnnotation class]]) {
        return nil;
    }

    static NSString *annotationIdentifier = @"StickerPin";

    MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationIdentifier];

    if (!annotationView) {
        annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIdentifier];
        annotationView.canShowCallout = YES;
    }

    TCircleView* circleView = [[TCircleView alloc] init];
    circleView.green = [postObj[@"severity"] floatValue]; //dynamic value coming from server

    UIImageView* imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Piano"]];

    CGRect r = imgView.frame;
    r.size.height = r.size.width = 60;
    imgView.frame = r;
    circleView.frame = r;

    [annotationView addSubview:circleView];
    [annotationView addSubview:imgView];

    return annotationView;
}

无法显示标注或甚至调用委托"didSelectAnnotationView:",如何在地图上显示自定义视图作为标注?

3个回答

14

MKAnnotationView 的默认 widthheight 均为 0。
这很可能会阻止它对触摸作出响应。

通常,如果设置了其 image 属性,则其 frame 将自动设置。

由于您未设置 image 而是添加子视图,因此请尝试手动将 frame 设置为至少与其最大子视图一样大。

例如:

imgView.frame = r;
circleView.frame = r;
annotationView.frame = r;  // <-- add this line

该死,希望这能够起作用。这是我见过的关于这个问题唯一的见解@Anna - SleepsOnNewspapers
这解决了我的问题 :-) 我在注释视图前面放了一个 imageView,它没有响应点击..现在它可以工作了!谢谢 Anna - Nicolai Harbo

3

我创建了一个注释视图的子类并实现了它。下面是代码:

@interface TStickerAnnotationView : MKAnnotationView

@property(nonatomic) float stickerColor;

@end

@interface TStickerAnnotationView () {
    UIImageView *_imageView;
    TCircleView *_circleView;
}


@end

@implementation TStickerAnnotationView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // make sure the x and y of the CGRect are half it's
        // width and height, so the callout shows when user clicks
        // in the middle of the image
        CGRect  viewRect = CGRectMake(-30, -30, 60, 60);

        TCircleView* circleView = [[TCircleView alloc] initWithFrame:viewRect];
        _circleView = circleView;
        [self addSubview:circleView];

        UIImageView* imageView = [[UIImageView alloc] initWithFrame:viewRect];

        // keeps the image dimensions correct
        // so if you have a rectangle image, it will show up as a rectangle,
        // instead of being resized into a square
        imageView.contentMode = UIViewContentModeScaleAspectFit;

        _imageView = imageView;

        [self addSubview:imageView];



    }
    return self;
}

- (void)setImage:(UIImage *)image
{
    // when an image is set for the annotation view,
    // it actually adds the image to the image view
    _imageView.image = image;
}

- (void)stickerColor:(float)color {
    _circleView.green = color;
    [_circleView setNeedsDisplay];
}

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接