// // MyAnimatedAnnotationView.m // IphoneMapSdkDemo // // Created by wzy on 14-11-27. // Copyright (c) 2014年 Baidu. All rights reserved. // #import "MyAnimatedAnnotationView.h" @interface MyAnimatedAnnotationView () { UILabel * labelNo; UILabel * labelStationName; UILabel * labelSumNum; UILabel * labelBicycleNum; UIView * areaPaoView; } @end @implementation MyAnimatedAnnotationView +(instancetype)annotationViewWithMapView:(BMKMapView *)mapView Annotation:(BMKPointAnnotation *)annotation{ MyAnimatedAnnotationView *view = (MyAnimatedAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"MyAnimatedAnnotationView"]; if (!view) { view = [[MyAnimatedAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"MyAnimatedAnnotationView"]; }else{ NSLog(@"我是复用的MyAnnotationView:%@",view.annotationImageView); //复用的时候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, 80.f, 43.f)]; [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?COLOR_THEME:[UIColor lightGrayColor]; labelNo = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 30)]; labelNo.textColor = [UIColor blackColor]; labelNo.font = [UIFont systemFontOfSize:15]; labelNo.backgroundColor = [UIColor clearColor]; labelNo.alpha = alpha; [areaPaoView addSubview:labelNo]; labelStationName = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 200, 30)]; labelStationName.textColor = [UIColor blackColor]; labelStationName.font = [UIFont systemFontOfSize:15]; labelStationName.backgroundColor = [UIColor clearColor]; labelStationName.alpha = alpha; [areaPaoView addSubview:labelStationName]; labelSumNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 200, 30)]; labelSumNum.textColor = [UIColor blackColor]; labelSumNum.font = [UIFont systemFontOfSize:15]; labelSumNum.backgroundColor = [UIColor clearColor]; labelSumNum.alpha = alpha; [areaPaoView addSubview:labelSumNum]; labelBicycleNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, 200, 30)]; labelBicycleNum.textColor = [UIColor blackColor]; labelBicycleNum.font = [UIFont systemFontOfSize:15]; labelBicycleNum.backgroundColor = [UIColor clearColor]; labelBicycleNum.alpha = alpha; [areaPaoView addSubview:labelBicycleNum]; // UILabel * labelSch = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 200, 30)]; // labelSch.text = [NSString stringWithFormat:@"所属驾校:%@",dic[@"SCHOOLNAME"]]; // labelSch.textColor = [UIColor blackColor]; // labelSch.backgroundColor = [UIColor clearColor]; // labelSch.alpha = alpha; // [areaPaoView addSubview:labelSch]; // // // UILabel * labelDqQx = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 200, 30)]; // labelDqQx.text = [NSString stringWithFormat:@"所属地区:%@",dic[@"TERMTYPE"]]; // labelDqQx.textColor = [UIColor blackColor]; // labelDqQx.backgroundColor = [UIColor clearColor]; // labelDqQx.alpha = alpha; // [areaPaoView addSubview:labelDqQx]; BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView: areaPaoView]; self.paopaoView=paopao; } return self; } -(void)setDic:(NSDictionary *)dic{ self.annotationImageView.image = [self getImgWithAnnottation:self.annotation]; //这个复用的时候paopaoView=nil,所以新建 NSArray *arr = [self.annotation.title componentsSeparatedByString:@"and"]; BOOL isOn = [arr[1] integerValue] == 1; areaPaoView.backgroundColor = isOn?COLOR_THEME:[UIColor lightGrayColor]; BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView: areaPaoView]; self.paopaoView=paopao; labelNo.text =[NSString stringWithFormat:@"车牌号码:%@",dic[@"LICNUM"]]; NSString *status = [NSString stringWithFormat:@"%@",dic[@"ISCONNECT"]]; NSString *isConnect = [status isEqualToString:@"1"] ? @"在线" : @"离线"; labelStationName.text = [NSString stringWithFormat:@"在线状态:%@",isConnect]; NSString *regStatus = [NSString stringWithFormat:@"%@",dic[@"REGSTATUS"]]; NSString *isReg = [regStatus isEqualToString:@"1"] ? @"已注册" : @"未注册"; labelSumNum.text = [NSString stringWithFormat:@"注册状态:%@",isReg]; NSString *terStatus = [NSString stringWithFormat:@"%@",dic[@"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:@"job_carOn"] : [UIImage imageNamed:@"job_carOff"]; [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