// // MyAnnotationView.m // IphoneMapSdkDemo // // Created by wzy on 14-11-27. // Copyright (c) 2014年 Baidu. All rights reserved. // #import "MyAnnotationView.h" @interface MyAnnotationView () { UILabel * labelNo; UILabel * labelStationName; UILabel * labelSumNum; UILabel * labelBicycleNum; UIView * areaPaoView; } @end @implementation MyAnnotationView +(instancetype)annotationViewWithMapView:(BMKMapView *)mapView Annotation:(BMKPointAnnotation *)annotation{ MyAnnotationView *view = (MyAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"MyAnnotationView"]; if (!view) { view = [[MyAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"MyAnnotationView"]; }else{ NSLog(@"我是复用的MyAnnotationView:%@",view.annotationImageView); //复用的时候paopaoView == nil;2017/9/18 //关于复用,参数annnotion+reuseId决定一个复用对象。测试结果:点聚合的时候会有部分复用,非点聚合暂时未体会到 } 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, 80.f, 43.f)]; self.canShowCallout = YES; self.label.hidden = YES; if (!_annotationImageView) { [self setBackgroundColor:[UIColor clearColor]]; _annotationImageView = [[UIImageView alloc]initWithFrame:self.bounds]; [self addSubview:_annotationImageView]; //自定义内容气泡 CGFloat alpha = 1.0; NSArray *arr = [self.annotation.title componentsSeparatedByString:@"and"]; BOOL isOn = [arr[1] integerValue] == 1; areaPaoView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 90)]; areaPaoView.layer.cornerRadius=8; areaPaoView.layer.masksToBounds=YES; areaPaoView.backgroundColor = isOn?RQMianColor:[UIColor lightGrayColor]; labelNo = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 30)]; labelNo.font = [UIFont systemFontOfSize:Font14]; labelNo.textColor = [UIColor whiteColor]; labelNo.backgroundColor = [UIColor clearColor]; labelNo.alpha = alpha; [areaPaoView addSubview:labelNo]; labelStationName = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 200, 30)]; labelStationName.font = [UIFont systemFontOfSize:Font14]; labelStationName.textColor = [UIColor whiteColor]; labelStationName.backgroundColor = [UIColor clearColor]; labelStationName.alpha = alpha; [areaPaoView addSubview:labelStationName]; labelSumNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 200, 30)]; labelSumNum.font = [UIFont systemFontOfSize:Font14]; labelSumNum.textColor = [UIColor whiteColor]; labelSumNum.backgroundColor = [UIColor clearColor]; labelSumNum.alpha = alpha; [areaPaoView addSubview:labelSumNum]; labelBicycleNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, 200, 30)]; labelBicycleNum.font = [UIFont systemFontOfSize:Font14]; labelBicycleNum.textColor = [UIColor whiteColor]; labelBicycleNum.backgroundColor = [UIColor clearColor]; labelBicycleNum.alpha = alpha; [areaPaoView addSubview:labelBicycleNum]; } //测试一共四个点,测试的时候几乎全由上面if里面重建_annotationImageView _annotationImageView.hidden = NO; _annotationImageView.image = [self getImgWithAnnottation:self.annotation]; //这个复用的时候paopaoView=nil,所以新建 NSArray *arr = [self.annotation.title componentsSeparatedByString:@"and"]; BOOL isOn = [arr[1] integerValue] == 1; areaPaoView.backgroundColor = isOn?RQMianColor:[UIColor lightGrayColor]; 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 = RQMianColor; _label.text = [NSString stringWithFormat:@"%ld", (long)size]; } -(void)setDic:(NSDictionary *)dic{ _dic = dic; labelNo.text =[NSString stringWithFormat:@"车牌号码:%@",dic[@"TCO_LICNUM"]]; NSString *status = [NSString stringWithFormat:@"%@",dic[@"TDI_IS_CONNECT"]]; NSString *isConnect = [status isEqualToString:@"1"] ? @"在线" : @"离线"; labelStationName.text = [NSString stringWithFormat:@"在线状态:%@",isConnect]; NSString *regStatus = [NSString stringWithFormat:@"%@",dic[@"TDI_REG_STATUS"]]; NSString *isReg = [regStatus isEqualToString:@"1"] ? @"已注册" : @"未注册"; labelSumNum.text = [NSString stringWithFormat:@"注册状态:%@",isReg]; NSString *terStatus = [NSString stringWithFormat:@"%@",dic[@"TDI_TERMTYPE"]]; NSString *isStu = @""; //1:车载计程计时终端; 2:课堂教学计时终端; 3:模拟训练计时终端; 0 教练APP设备 10学员APP设备 switch ([terStatus integerValue]) { case 0: isStu = @"教练APP设备"; break; case 1: isStu = @"车载计程计时终端"; break; case 2: isStu = @"课堂教学计时终端"; break; case 3: isStu = @"模拟训练计时终端"; break; case 10: isStu = @"学员APP设备"; break; default: break; } labelBicycleNum.text = [NSString stringWithFormat:@"终端类型:%@",isStu]; } -(UIImage *)getImgWithAnnottation:(BMKPointAnnotation *)annotation{ NSArray *arr = [annotation.title componentsSeparatedByString:@"and"]; UIView *pointV = [[UIView alloc]initWithFrame:CGRectMake(0.f, 0.f, 80.f, 38.f)]; UIImageView *annotationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(27, 0, 25, 25)]; annotationImageView.image = [arr[1] integerValue] == 1 ? [UIImage imageNamed:@"annotationOn"] : [UIImage imageNamed:@"annotationOff"]; [pointV addSubview:annotationImageView]; UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, 80, 17)]; label.text = arr[0]; label.numberOfLines = 2; label.backgroundColor=[UIColor clearColor]; label.font = [UIFont systemFontOfSize:12]; label.textAlignment = NSTextAlignmentCenter; label.textColor = [UIColor blackColor];//UIColor colorWithWhite:0.3 alpha:1.0 // CGSize size = [label sizeThatFits:CGSizeMake(label.frame.size.width, MAXFLOAT)]; // label.frame =CGRectMake(0, 25, 80, 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; } @end