NeighbourBMKAnnotationView.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // NeighbourBMKAnnotationView.m
  3. // jiaPei
  4. //
  5. // Created by EchoShacolee on 2017/7/31.
  6. // Copyright © 2017年 JCZ. All rights reserved.
  7. //
  8. #import "NeighbourBMKAnnotationView.h"
  9. @interface NeighbourBMKAnnotationView()
  10. {
  11. UIView * areaPaoView;
  12. UILabel *driverName;
  13. UILabel *carName;
  14. }
  15. @property (nonatomic, strong) UILabel *label;
  16. @property (nonatomic, strong) UIImageView *annotationImageView;
  17. @end
  18. @implementation NeighbourBMKAnnotationView
  19. +(instancetype)annotationViewWithMapView:(BMKMapView *)mapView Annotation:(BMKPointAnnotation *)annotation{
  20. NeighbourBMKAnnotationView *view = (NeighbourBMKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"MyAnnotationView"];
  21. if (!view) {
  22. view = [[NeighbourBMKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"MyAnnotationView"];
  23. }else{
  24. // NSLog(@"我是复用的MyAnnotationView:我的annotation%@,传入的annotation%@",view.annotation,annotation.title);
  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, 22.f, 22.f)];
  33. _label = [[UILabel alloc] initWithFrame:CGRectMake(0.f, 0.f, 22.f, 22.f)];
  34. _label.textColor = [UIColor whiteColor];
  35. _label.font = [UIFont systemFontOfSize:11];
  36. _label.textAlignment = NSTextAlignmentCenter;
  37. _label.layer.masksToBounds = YES;
  38. _label.layer.cornerRadius = 11;
  39. [self addSubview:_label];
  40. self.alpha = 0.85;
  41. }
  42. return self;
  43. }
  44. - (void)setSize:(NSInteger)size {
  45. _size = size;
  46. if (_size == 1) {
  47. [self setBounds:CGRectMake(0.f, 0.f, 125.f, 60.f)];
  48. self.canShowCallout = YES;
  49. self.label.hidden = YES;
  50. if (!_annotationImageView) {
  51. [self setBackgroundColor:[UIColor clearColor]];
  52. _annotationImageView = [[UIImageView alloc]initWithFrame:self.bounds];
  53. [self addSubview:_annotationImageView];
  54. //自定义内容气泡
  55. UIView *popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 230, 60)];
  56. areaPaoView = popView;
  57. popView.backgroundColor = [UIColor whiteColor];
  58. [popView.layer setMasksToBounds:YES];
  59. [popView.layer setCornerRadius:3.0];
  60. popView.alpha = 0.9;
  61. //自定义气泡的内容,添加子控件在popView上
  62. driverName = [[UILabel alloc]initWithFrame:CGRectMake(8, 4, 170, 30)];
  63. driverName.numberOfLines = 0;
  64. driverName.backgroundColor = [UIColor clearColor];
  65. driverName.font = [UIFont systemFontOfSize:15];
  66. driverName.textColor = [UIColor blackColor];
  67. driverName.textAlignment = NSTextAlignmentLeft;
  68. [popView addSubview:driverName];
  69. if (self.annotation.subtitle == nil) {
  70. driverName.frame = CGRectMake(8, 4, 170, 60);
  71. }else{
  72. carName = [[UILabel alloc]initWithFrame:CGRectMake(8, 30, 170, 30)];
  73. carName.text = self.annotation.subtitle;
  74. carName.backgroundColor = [UIColor clearColor];
  75. carName.font = [UIFont systemFontOfSize:11];
  76. carName.textColor = [UIColor lightGrayColor];
  77. carName.textAlignment = NSTextAlignmentLeft;
  78. [popView addSubview:carName];
  79. }
  80. UIButton *searchBn = [[UIButton alloc]initWithFrame:CGRectMake(180, 0, 50, 60)];
  81. [searchBn setTitle:@"查看路线" forState:UIControlStateNormal];
  82. searchBn.backgroundColor = defGreen;
  83. searchBn.titleLabel.numberOfLines = 0;
  84. [searchBn addTarget:self action:@selector(searchLine) forControlEvents:UIControlEventTouchUpInside];
  85. [popView addSubview:searchBn];
  86. }
  87. driverName.text = self.annotation.title;
  88. carName.text = self.annotation.subtitle;
  89. _annotationImageView.hidden = NO;
  90. _annotationImageView.image = [self getImgWithAnnottation:(BMKPointAnnotation *)self.annotation];
  91. //这个复用的时候paopaoView=nil,所以新建
  92. BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView: areaPaoView];
  93. self.paopaoView=paopao;
  94. return;
  95. }
  96. if (_annotationImageView) {
  97. _annotationImageView.hidden = YES;
  98. }
  99. [self setBounds:CGRectMake(0.f, 0.f, 22.f, 22.f)];
  100. self.canShowCallout = NO;
  101. self.label.hidden = NO;
  102. self.label.backgroundColor = defGreen;
  103. _label.text = [NSString stringWithFormat:@"%ld", size];
  104. }
  105. -(UIImage *)getImgWithAnnottation:(BMKPointAnnotation *)annotation{
  106. UIView *pointV = [[UIView alloc]initWithFrame:self.bounds];
  107. UIImageView *annotationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 0, 25, 25)];
  108. annotationImageView.image = [UIImage imageNamed:@"neighbour_adress"];
  109. [pointV addSubview:annotationImageView];
  110. UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 25, 125, 35)];
  111. if (self.annotation.subtitle == nil) {
  112. label.text=[NSString stringWithFormat:@"%@",annotation.title];
  113. }else{
  114. label.text=[NSString stringWithFormat:@"%@(%@)",annotation.title,annotation.subtitle];
  115. }
  116. label.numberOfLines = 2;
  117. label.backgroundColor=[UIColor clearColor];
  118. label.font = [UIFont systemFontOfSize:12];
  119. label.textAlignment = NSTextAlignmentCenter;
  120. label.textColor = [UIColor colorWithWhite:0.3 alpha:1.0];
  121. CGSize size = [label sizeThatFits:CGSizeMake(label.frame.size.width, MAXFLOAT)];
  122. label.frame =CGRectMake(0, 25, 125, size.height);
  123. [pointV addSubview:label];
  124. return [self convertViewToImage:pointV];
  125. }
  126. -(UIImage*)convertViewToImage:(UIView*)v{
  127. CGSize s = v.bounds.size;
  128. // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了
  129. UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
  130. [v.layer renderInContext:UIGraphicsGetCurrentContext()];
  131. UIImage*image = UIGraphicsGetImageFromCurrentImageContext();
  132. UIGraphicsEndImageContext();
  133. return image;
  134. }
  135. - (void)searchLine {
  136. // ShowMsgUnOpen();
  137. if (self.block) {
  138. self.block(self.annotation);
  139. }
  140. }
  141. @end