jon*_*ypz 2 iphone calayer mkmapview mkannotationview ios
所以我试图复制以下场景(半透明注释视图):

我尝试了以下实现失败:
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;
Run Code Online (Sandbox Code Playgroud)
}`
然后将其添加到 - (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;
}
Run Code Online (Sandbox Code Playgroud)
我认为在注释之上添加MKOverlays可能是一种解决方法,但它不应该是我认为的方式.
有没有人对如何实现这个有其他建议?
创建UIImageView对象并使其看起来像您需要的图像.
在viewForAnnotationdelegate方法中添加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;
}
Run Code Online (Sandbox Code Playgroud)
