【问题标题】:How to add custom transparent MKAnnotation to MKMapView?如何将自定义透明 MKAnnotation 添加到 MKMapView?
【发布时间】:2015-02-15 10:36:19
【问题描述】:

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

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

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

代码:

-(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 可能是一种解决方法,但我相信它不应该是要走的路。

是否有人对如何实现此功能有其他建议?

【问题讨论】:

    标签: ios iphone mkmapview calayer mkannotationview


    【解决方案1】:

    我要做的是在 Photoshop 中创建一个具有透明背景的图像,然后在顶部添加所需的黄色圆圈。然后将该圆圈的不透明度设置为您想要的任何不透明度。将图像另存为 PNG。

    保存图像后,将其添加到您的 Xcode 项目中。添加后,在viewForAnnotation 下添加以下行。

    annotationView.image = [UIImage imageNamed:@"ThisIsThePNGImagesName.png"];
    

    希望有所帮助:)

    【讨论】:

    • 这正是我在第一个实现中所做的(检查上面) - 它没有工作。
    【解决方案2】:

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

    viewForAnnotation委托方法中添加为annotationView的子视图就可以了。

    您还需要为注释图像设置中心位置偏移,以使注释完全正确的位置。

    看看下面的代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多