所以我尝试复制以下场景(半透明注释 View ):
我尝试了以下实现,但没有成功:
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;
}
关于ios - 如何将自定义透明MKAnnotation添加到MKMapView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27516112/