gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:26:52 25 4
gpt4 key购买 nike

所以我试图复制以下场景(半透明注释 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com