// // NeighbourBMKAnnotationView.m // jiaPei // // Created by EchoShacolee on 2017/7/31. // Copyright © 2017年 JCZ. All rights reserved. // #import "NeighbourBMKAnnotationView.h" @interface NeighbourBMKAnnotationView() { UIView * areaPaoView; UILabel *driverName; UILabel *carName; } @property (nonatomic, strong) UILabel *label; @property (nonatomic, strong) UIImageView *annotationImageView; @end @implementation NeighbourBMKAnnotationView +(instancetype)annotationViewWithMapView:(BMKMapView *)mapView Annotation:(BMKPointAnnotation *)annotation{ NeighbourBMKAnnotationView *view = (NeighbourBMKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"MyAnnotationView"]; if (!view) { view = [[NeighbourBMKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"MyAnnotationView"]; }else{ // NSLog(@"我是复用的MyAnnotationView:我的annotation%@,传入的annotation%@",view.annotation,annotation.title); //复用的时候paopaoView == nil;2017/9/18 } return view; } - (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]; if (self) { [self setBounds:CGRectMake(0.f, 0.f, 22.f, 22.f)]; _label = [[UILabel alloc] initWithFrame:CGRectMake(0.f, 0.f, 22.f, 22.f)]; _label.textColor = [UIColor whiteColor]; _label.font = [UIFont systemFontOfSize:11]; _label.textAlignment = NSTextAlignmentCenter; _label.layer.masksToBounds = YES; _label.layer.cornerRadius = 11; [self addSubview:_label]; self.alpha = 0.85; } return self; } - (void)setSize:(NSInteger)size { _size = size; if (_size == 1) { [self setBounds:CGRectMake(0.f, 0.f, 125.f, 60.f)]; self.canShowCallout = YES; self.label.hidden = YES; if (!_annotationImageView) { [self setBackgroundColor:[UIColor clearColor]]; _annotationImageView = [[UIImageView alloc]initWithFrame:self.bounds]; [self addSubview:_annotationImageView]; //自定义内容气泡 UIView *popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 230, 60)]; areaPaoView = popView; popView.backgroundColor = [UIColor whiteColor]; [popView.layer setMasksToBounds:YES]; [popView.layer setCornerRadius:3.0]; popView.alpha = 0.9; //自定义气泡的内容,添加子控件在popView上 driverName = [[UILabel alloc]initWithFrame:CGRectMake(8, 4, 170, 30)]; driverName.numberOfLines = 0; driverName.backgroundColor = [UIColor clearColor]; driverName.font = [UIFont systemFontOfSize:15]; driverName.textColor = [UIColor blackColor]; driverName.textAlignment = NSTextAlignmentLeft; [popView addSubview:driverName]; if (self.annotation.subtitle == nil) { driverName.frame = CGRectMake(8, 4, 170, 60); }else{ carName = [[UILabel alloc]initWithFrame:CGRectMake(8, 30, 170, 30)]; carName.text = self.annotation.subtitle; carName.backgroundColor = [UIColor clearColor]; carName.font = [UIFont systemFontOfSize:11]; carName.textColor = [UIColor lightGrayColor]; carName.textAlignment = NSTextAlignmentLeft; [popView addSubview:carName]; } UIButton *searchBn = [[UIButton alloc]initWithFrame:CGRectMake(180, 0, 50, 60)]; [searchBn setTitle:@"查看路线" forState:UIControlStateNormal]; searchBn.backgroundColor = defGreen; searchBn.titleLabel.numberOfLines = 0; [searchBn addTarget:self action:@selector(searchLine) forControlEvents:UIControlEventTouchUpInside]; [popView addSubview:searchBn]; } driverName.text = self.annotation.title; carName.text = self.annotation.subtitle; _annotationImageView.hidden = NO; _annotationImageView.image = [self getImgWithAnnottation:(BMKPointAnnotation *)self.annotation]; //这个复用的时候paopaoView=nil,所以新建 BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView: areaPaoView]; self.paopaoView=paopao; return; } if (_annotationImageView) { _annotationImageView.hidden = YES; } [self setBounds:CGRectMake(0.f, 0.f, 22.f, 22.f)]; self.canShowCallout = NO; self.label.hidden = NO; self.label.backgroundColor = defGreen; _label.text = [NSString stringWithFormat:@"%ld", size]; } -(UIImage *)getImgWithAnnottation:(BMKPointAnnotation *)annotation{ UIView *pointV = [[UIView alloc]initWithFrame:self.bounds]; UIImageView *annotationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 0, 25, 25)]; annotationImageView.image = [UIImage imageNamed:@"neighbour_adress"]; [pointV addSubview:annotationImageView]; UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 25, 125, 35)]; if (self.annotation.subtitle == nil) { label.text=[NSString stringWithFormat:@"%@",annotation.title]; }else{ label.text=[NSString stringWithFormat:@"%@(%@)",annotation.title,annotation.subtitle]; } label.numberOfLines = 2; label.backgroundColor=[UIColor clearColor]; label.font = [UIFont systemFontOfSize:12]; label.textAlignment = NSTextAlignmentCenter; label.textColor = [UIColor colorWithWhite:0.3 alpha:1.0]; CGSize size = [label sizeThatFits:CGSizeMake(label.frame.size.width, MAXFLOAT)]; label.frame =CGRectMake(0, 25, 125, size.height); [pointV addSubview:label]; return [self convertViewToImage:pointV]; } -(UIImage*)convertViewToImage:(UIView*)v{ CGSize s = v.bounds.size; // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了 UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale); [v.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage*image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } - (void)searchLine { // ShowMsgUnOpen(); if (self.block) { self.block(self.annotation); } } @end