MyAnnotationView.m 8.2 KB

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