MyAnimatedAnnotationView.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // MyAnimatedAnnotationView.m
  3. // IphoneMapSdkDemo
  4. //
  5. // Created by wzy on 14-11-27.
  6. // Copyright (c) 2014年 Baidu. All rights reserved.
  7. //
  8. #import "MyAnimatedAnnotationView.h"
  9. @interface MyAnimatedAnnotationView ()
  10. {
  11. UILabel * labelNo;
  12. UILabel * labelStationName;
  13. UILabel * labelSumNum;
  14. UILabel * labelBicycleNum;
  15. UIView * areaPaoView;
  16. }
  17. @end
  18. @implementation MyAnimatedAnnotationView
  19. +(instancetype)annotationViewWithMapView:(BMKMapView *)mapView Annotation:(BMKPointAnnotation *)annotation{
  20. MyAnimatedAnnotationView *view = (MyAnimatedAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"MyAnimatedAnnotationView"];
  21. if (!view) {
  22. view = [[MyAnimatedAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"MyAnimatedAnnotationView"];
  23. }else{
  24. NSLog(@"我是复用的MyAnnotationView:%@",view.annotationImageView);
  25. //复用的时候paopaoView == nil;2017/9/18
  26. }
  27. return view;
  28. }
  29. - (id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
  30. self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
  31. if (self) {
  32. [self setBounds:CGRectMake(0.f, 0.f, 80.f, 43.f)];
  33. [self setBackgroundColor:[UIColor clearColor]];
  34. _annotationImageView = [[UIImageView alloc]initWithFrame:self.bounds];
  35. [self addSubview:_annotationImageView];
  36. //自定义内容气泡
  37. CGFloat alpha = 1.0;
  38. NSArray *arr = [self.annotation.title componentsSeparatedByString:@"and"];
  39. BOOL isOn = [arr[1] integerValue] == 1;
  40. areaPaoView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 90)];
  41. areaPaoView.layer.cornerRadius=8;
  42. areaPaoView.layer.masksToBounds=YES;
  43. areaPaoView.backgroundColor = isOn?COLOR_THEME:[UIColor lightGrayColor];
  44. UIColor *textColor = [UIColor whiteColor];
  45. labelNo = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 30)];
  46. labelNo.textColor = textColor;
  47. labelNo.font = [UIFont systemFontOfSize:15];
  48. labelNo.backgroundColor = [UIColor clearColor];
  49. labelNo.alpha = alpha;
  50. [areaPaoView addSubview:labelNo];
  51. labelStationName = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 200, 30)];
  52. labelStationName.textColor = textColor;
  53. labelStationName.font = [UIFont systemFontOfSize:15];
  54. labelStationName.backgroundColor = [UIColor clearColor];
  55. labelStationName.alpha = alpha;
  56. [areaPaoView addSubview:labelStationName];
  57. labelSumNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 200, 30)];
  58. labelSumNum.textColor = textColor;
  59. labelSumNum.font = [UIFont systemFontOfSize:15];
  60. labelSumNum.backgroundColor = [UIColor clearColor];
  61. labelSumNum.alpha = alpha;
  62. [areaPaoView addSubview:labelSumNum];
  63. labelBicycleNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, 200, 30)];
  64. labelBicycleNum.textColor = textColor;
  65. labelBicycleNum.font = [UIFont systemFontOfSize:15];
  66. labelBicycleNum.backgroundColor = [UIColor clearColor];
  67. labelBicycleNum.alpha = alpha;
  68. [areaPaoView addSubview:labelBicycleNum];
  69. // UILabel * labelSch = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 200, 30)];
  70. // labelSch.text = [NSString stringWithFormat:@"所属驾校:%@",dic[@"SCHOOLNAME"]];
  71. // labelSch.textColor = [UIColor blackColor];
  72. // labelSch.backgroundColor = [UIColor clearColor];
  73. // labelSch.alpha = alpha;
  74. // [areaPaoView addSubview:labelSch];
  75. //
  76. //
  77. // UILabel * labelDqQx = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 200, 30)];
  78. // labelDqQx.text = [NSString stringWithFormat:@"所属地区:%@",dic[@"TERMTYPE"]];
  79. // labelDqQx.textColor = [UIColor blackColor];
  80. // labelDqQx.backgroundColor = [UIColor clearColor];
  81. // labelDqQx.alpha = alpha;
  82. // [areaPaoView addSubview:labelDqQx];
  83. BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView:
  84. areaPaoView];
  85. self.paopaoView=paopao;
  86. }
  87. return self;
  88. }
  89. -(void)setDic:(NSDictionary *)dic{
  90. self.annotationImageView.image = [self getImgWithAnnottation:self.annotation];
  91. //这个复用的时候paopaoView=nil,所以新建
  92. NSArray *arr = [self.annotation.title componentsSeparatedByString:@"and"];
  93. BOOL isOn = [arr[1] integerValue] == 1;
  94. areaPaoView.backgroundColor = isOn?COLOR_THEME:[UIColor lightGrayColor];
  95. BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView: areaPaoView];
  96. self.paopaoView=paopao;
  97. labelNo.text =[NSString stringWithFormat:@"车牌号码:%@",dic[@"LICNUM"]];
  98. NSString *status = [NSString stringWithFormat:@"%@",dic[@"ISCONNECT"]];
  99. NSString *isConnect = [status isEqualToString:@"1"] ? @"在线" : @"离线";
  100. labelStationName.text = [NSString stringWithFormat:@"在线状态:%@",isConnect];
  101. NSString *regStatus = [NSString stringWithFormat:@"%@",dic[@"REGSTATUS"]];
  102. NSString *isReg = [regStatus isEqualToString:@"1"] ? @"已注册" : @"未注册";
  103. labelSumNum.text = [NSString stringWithFormat:@"注册状态:%@",isReg];
  104. NSString *terStatus = [NSString stringWithFormat:@"%@",dic[@"TERMTYPE"]];
  105. NSString *isStu = @"";
  106. //1:车载计程计时终端; 2:课堂教学计时终端; 3:模拟训练计时终端; 0 教练APP设备 10学员APP设备
  107. switch ([terStatus integerValue]) {
  108. case 0:
  109. isStu = @"教练APP设备";
  110. break;
  111. case 1:
  112. isStu = @"车载计程计时终端";
  113. break;
  114. case 2:
  115. isStu = @"课堂教学计时终端";
  116. break;
  117. case 3:
  118. isStu = @"模拟训练计时终端";
  119. break;
  120. case 10:
  121. isStu = @"学员APP设备";
  122. break;
  123. default:
  124. break;
  125. }
  126. labelBicycleNum.text = [NSString stringWithFormat:@"终端类型:%@",isStu];
  127. }
  128. -(UIImage *)getImgWithAnnottation:(BMKPointAnnotation *)annotation{
  129. NSArray *arr = [annotation.title componentsSeparatedByString:@"and"];
  130. UIView *pointV = [[UIView alloc]initWithFrame:CGRectMake(0.f, 0.f, 80.f, 38.f)];
  131. UIImageView *annotationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(27, 0, 25, 25)];
  132. annotationImageView.image = [arr[1] integerValue] == 1 ? [UIImage imageNamed:@"job_carOn"] : [UIImage imageNamed:@"job_carOff"];
  133. [pointV addSubview:annotationImageView];
  134. UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, 80, 17)];
  135. label.text = arr[0];
  136. label.numberOfLines = 2;
  137. label.backgroundColor=[UIColor clearColor];
  138. label.font = [UIFont systemFontOfSize:12];
  139. label.textAlignment = NSTextAlignmentCenter;
  140. label.textColor = [UIColor blackColor];//UIColor colorWithWhite:0.3 alpha:1.0
  141. // CGSize size = [label sizeThatFits:CGSizeMake(label.frame.size.width, MAXFLOAT)];
  142. // label.frame =CGRectMake(0, 25, 80, size.height);
  143. [pointV addSubview:label];
  144. return [self convertViewToImage:pointV];
  145. }
  146. -(UIImage*)convertViewToImage:(UIView*)v{
  147. CGSize s = v.bounds.size;
  148. // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了
  149. UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
  150. [v.layer renderInContext:UIGraphicsGetCurrentContext()];
  151. UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
  152. UIGraphicsEndImageContext();
  153. return image;
  154. }
  155. @end