ios - 如何将自定义透明MKAnnotation添加到MKMapView?

标签 ios iphone mkmapview calayer mkannotationview

所以我尝试复制以下场景(半透明注释 View ):

enter image description here

我尝试了以下实现,但没有成功:

1- 创建不透明度为 30% 的自定义图像并将其添加到 map ---> 结果:图像保持不透明。

代码:

-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{

self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {

    LLAnnotation *myA = (LLAnnotation*) annotation;

    self.accessibilityLabel = myA.title;
    self.annotation = myA;
    self.enabled = YES;
    self.canShowCallout = YES;

    self.centerOffset = CGPointMake(5,-10);

    self.backgroundColor = [UIColor yellowColor];

    self.image = [UIImage imageNamed:@"circle"];
}

return self;

}`

然后将其添加到 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation_

2- 将子图层添加到 AnnotationView 并清除它 ---> 结果:不显示任何注释。

代码:

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

    if (annotation_ == mapView.userLocation) return nil;

    MKAnnotationView *m = [[MKAnnotationView alloc] initWithAnnotation:annotation_ reuseIdentifier:@"default"];

//    m.backgroundColor = [UIColor clearColor];

    CALayer *layer = [[CALayer alloc]init];

    layer.frame = m.frame;

    layer.backgroundColor = [UIColor lightGreenColor].CGColor;

    [m.layer addSublayer:layer];

    m.layer.cornerRadius = m.frame.size.width/2;
    m.layer.borderWidth = 2;
    m.layer.masksToBounds = YES;

    return m;
}

我认为在注释之上添加 MKOverlays 可能是一种解决方法,但我认为这不应该是正确的方法。

有人对如何实现这一点有其他建议吗?

最佳答案

创建UIImageView对象并使其看起来像您需要的图像。

viewForAnnotation委托(delegate)方法中添加为annotationView的 subview 就可以了。

此外,您还需要设置注释图像的中心位置偏移量,以使注释准确地显示在正确的位置位置。

看看下面的代码:

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

    if (annotation_ == mapView.userLocation) return nil;

    MKAnnotationView *m = [[MKAnnotationView alloc] initWithAnnotation:annotation_ reuseIdentifier:@"default"];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(m.center.x, m.center.y, 20, 20)];
    [imageView setBackgroundColor:[UIColor colorWithRed:0.7294 green:0.7843 blue:0.1921 alpha:1.0]];

    imageView.layer.cornerRadius = imageView.frame.size.width / 2;
    imageView.alpha = 0.6f;
    [m addSubview:imageView];

    // Also set center offset for annotation  
    [m setCenterOffset:CGPointMake(-10, -20)];

    return m;
}

enter image description here

关于ios - 如何将自定义透明MKAnnotation添加到MKMapView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27516112/

相关文章:

ios - 带有 Core Audio AudioQueue 的 AVFoundation session

ios - Apple 的 SwiftUI 教程代码在自制副本中不显示 View 内容

ios - Google Analytics SDK iOS 中有哪些事件类别和操作?

iphone - 向 UIMapView 添加可点击注释

ios - 如何在 Swift iOS 中更改 MKUserLocation 注释中的默认图像

iphone - UITableView 更改标题标题颜色

iPhone Objective-C 以编程方式将范围按钮添加到 UISearchBar

javascript - 使用媒体查询添加类或其他属性

ios - 最近发布的 iPhone 应用程序更新卡在启动画面

swift - 自定义图像作为带有两个不同颜色图像的注释图钉