关于百度地图标注气泡框显示问题

  最近开发集成了百度地图,根据需求,我们需要在用户进入地图时就标注一系列的坐标,且所显示的标注必须全部显示气泡框。需求如此,开始我的思路时这样的:利用百度地图API提供的 [xxx setSelected:YES animated:NO];方法,我想便利所有的BMKPinAnnotationView,然后再全部用此方法显示气泡框,但最后测试发现,没有什么卵用,最后他只会在最后一个标注上显示气泡。

 在网上也没找到合适的方法,最后逛贴吧,偶然某位朋友提供的一个思路,即:把标注和气泡做成一体 ,这样等于标注显示了 ,气泡等于也一起显示了, 开始撸代码:

自定义BMKPinAnnotationView:

  - (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier {

self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

if (self) {

_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.f, 0.f, 60, 60)];

_titleLabel.textColor = [UIColor whiteColor];

_titleLabel.font = [UIFont systemFontOfSize:12];

_titleLabel.textAlignment = NSTextAlignmentCenter;

_titleLabel.numberOfLines = 2;

_titleLabel.backgroundColor = [UIColor colorWithRed:255/255.0 green:87/255.0 blue:138/255.0 alpha:0.9];

UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)];

// 2. 将点击事件添加到label上

[_titleLabel addGestureRecognizer:labelTapGestureRecognizer];

_titleLabel.userInteractionEnabled = YES; // 可以理解为设置label可被点击

[self addSubview:_titleLabel];

_imageView = [[UIImageView alloc] init];

_imageView.alpha = 0.9;

[self addSubview:_imageView];

_pharmacyLabel = [[UILabel alloc] init];

_pharmacyLabel.textColor = [UIColor whiteColor];

_pharmacyLabel.font = [UIFont systemFontOfSize:14];

_pharmacyLabel.textAlignment = NSTextAlignmentCenter;

_pharmacyLabel.numberOfLines = 0;

[_imageView addSubview:_pharmacyLabel];

}

return self;

}

- (void)layoutSubviews {

[super layoutSubviews];

CGSize imageSize = [self contentSize];

_imageView.frame = CGRectMake(0, 0, imageSize.width + 15,imageSize.height + 15 );

_pharmacyLabel.frame = CGRectMake(5, 5, imageSize.width ,imageSize.height);

}

- (CGSize)contentSize {

CGSize maxSize = CGSizeMake(ScreenSize.width *0.5, MAXFLOAT);

// 计算文字的高度

return  [_title boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size;

}

- (void)labelClick {

}

- (void)setSize:(NSInteger)size {

_size = size;

_titleLabel.hidden = YES;

_imageView.hidden = NO;

_pharmacyLabel.hidden = NO;

_pharmacyLabel.text = _title;

_imageView.image = [UIImage imageNamed:@"mapPopViewBGICon"];

}

- (void)setTitle:(NSString *)title {

_title = title;

}

- (void)setCluster:(XJCluster *)cluster {

_cluster = cluster;

}

其他的就是添加坐标啥的,没有什么,最后在你的代理方法- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation 返回你自定义的AnnotationView即可。

效果如下图:

关于百度地图标注气泡框显示问题_第1张图片
默认显示气泡

你可能感兴趣的:(关于百度地图标注气泡框显示问题)