MyAnimatedAnnotationView.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. labelNo = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 30)];
  45. labelNo.textColor = [UIColor blackColor];
  46. labelNo.font = [UIFont systemFontOfSize:15];
  47. labelNo.backgroundColor = [UIColor clearColor];
  48. labelNo.alpha = alpha;
  49. [areaPaoView addSubview:labelNo];
  50. labelStationName = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 200, 30)];
  51. labelStationName.textColor = [UIColor blackColor];
  52. labelStationName.font = [UIFont systemFontOfSize:15];
  53. labelStationName.backgroundColor = [UIColor clearColor];
  54. labelStationName.alpha = alpha;
  55. [areaPaoView addSubview:labelStationName];
  56. labelSumNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 40, 200, 30)];
  57. labelSumNum.textColor = [UIColor blackColor];
  58. labelSumNum.font = [UIFont systemFontOfSize:15];
  59. labelSumNum.backgroundColor = [UIColor clearColor];
  60. labelSumNum.alpha = alpha;
  61. [areaPaoView addSubview:labelSumNum];
  62. labelBicycleNum = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, 200, 30)];
  63. labelBicycleNum.textColor = [UIColor blackColor];
  64. labelBicycleNum.font = [UIFont systemFontOfSize:15];
  65. labelBicycleNum.backgroundColor = [UIColor clearColor];
  66. labelBicycleNum.alpha = alpha;
  67. [areaPaoView addSubview:labelBicycleNum];
  68. // UILabel * labelSch = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 200, 30)];
  69. // labelSch.text = [NSString stringWithFormat:@"所属驾校:%@",dic[@"SCHOOLNAME"]];
  70. // labelSch.textColor = [UIColor blackColor];
  71. // labelSch.backgroundColor = [UIColor clearColor];
  72. // labelSch.alpha = alpha;
  73. // [areaPaoView addSubview:labelSch];
  74. //
  75. //
  76. // UILabel * labelDqQx = [[UILabel alloc]initWithFrame:CGRectMake(10, 80, 200, 30)];
  77. // labelDqQx.text = [NSString stringWithFormat:@"所属地区:%@",dic[@"TERMTYPE"]];
  78. // labelDqQx.textColor = [UIColor blackColor];
  79. // labelDqQx.backgroundColor = [UIColor clearColor];
  80. // labelDqQx.alpha = alpha;
  81. // [areaPaoView addSubview:labelDqQx];
  82. BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView:
  83. areaPaoView];
  84. self.paopaoView=paopao;
  85. }
  86. return self;
  87. }
  88. -(void)setDic:(NSDictionary *)dic{
  89. self.annotationImageView.image = [self getImgWithAnnottation:self.annotation];
  90. //这个复用的时候paopaoView=nil,所以新建
  91. NSArray *arr = [self.annotation.title componentsSeparatedByString:@"and"];
  92. BOOL isOn = [arr[1] integerValue] == 1;
  93. areaPaoView.backgroundColor = isOn?COLOR_THEME:[UIColor lightGrayColor];
  94. BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView: areaPaoView];
  95. self.paopaoView=paopao;
  96. labelNo.text =[NSString stringWithFormat:@"车牌号码:%@",dic[@"LICNUM"]];
  97. NSString *status = [NSString stringWithFormat:@"%@",dic[@"ISCONNECT"]];
  98. NSString *isConnect = [status isEqualToString:@"1"] ? @"在线" : @"离线";
  99. labelStationName.text = [NSString stringWithFormat:@"在线状态:%@",isConnect];
  100. NSString *regStatus = [NSString stringWithFormat:@"%@",dic[@"REGSTATUS"]];
  101. NSString *isReg = [regStatus isEqualToString:@"1"] ? @"已注册" : @"未注册";
  102. labelSumNum.text = [NSString stringWithFormat:@"注册状态:%@",isReg];
  103. NSString *terStatus = [NSString stringWithFormat:@"%@",dic[@"TERMTYPE"]];
  104. NSString *isStu = @"";
  105. //1:车载计程计时终端; 2:课堂教学计时终端; 3:模拟训练计时终端; 0 教练APP设备 10学员APP设备
  106. switch ([terStatus integerValue]) {
  107. case 0:
  108. isStu = @"教练APP设备";
  109. break;
  110. case 1:
  111. isStu = @"车载计程计时终端";
  112. break;
  113. case 2:
  114. isStu = @"课堂教学计时终端";
  115. break;
  116. case 3:
  117. isStu = @"模拟训练计时终端";
  118. break;
  119. case 10:
  120. isStu = @"学员APP设备";
  121. break;
  122. default:
  123. break;
  124. }
  125. labelBicycleNum.text = [NSString stringWithFormat:@"终端类型:%@",isStu];
  126. }
  127. -(UIImage *)getImgWithAnnottation:(BMKPointAnnotation *)annotation{
  128. NSArray *arr = [annotation.title componentsSeparatedByString:@"and"];
  129. UIView *pointV = [[UIView alloc]initWithFrame:CGRectMake(0.f, 0.f, 80.f, 38.f)];
  130. UIImageView *annotationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(27, 0, 25, 25)];
  131. annotationImageView.image = [arr[1] integerValue] == 1 ? [UIImage imageNamed:@"job_carOn"] : [UIImage imageNamed:@"job_carOff"];
  132. [pointV addSubview:annotationImageView];
  133. UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, 80, 17)];
  134. label.text = arr[0];
  135. label.numberOfLines = 2;
  136. label.backgroundColor=[UIColor clearColor];
  137. label.font = [UIFont systemFontOfSize:12];
  138. label.textAlignment = NSTextAlignmentCenter;
  139. label.textColor = [UIColor blackColor];//UIColor colorWithWhite:0.3 alpha:1.0
  140. // CGSize size = [label sizeThatFits:CGSizeMake(label.frame.size.width, MAXFLOAT)];
  141. // label.frame =CGRectMake(0, 25, 80, size.height);
  142. [pointV addSubview:label];
  143. return [self convertViewToImage:pointV];
  144. }
  145. -(UIImage*)convertViewToImage:(UIView*)v{
  146. CGSize s = v.bounds.size;
  147. // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了
  148. UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
  149. [v.layer renderInContext:UIGraphicsGetCurrentContext()];
  150. UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
  151. UIGraphicsEndImageContext();
  152. return image;
  153. }
  154. @end