既然创建了标注,前面说需要加载自定义气泡,那也就需要去重写构造。调用下面的这个方法:
CustomAnnotationView是关于气泡的自定义样式,因为气泡本来是创建在标注的视图层上,所以在创建的时候需要去继承:MAAnnotationView这个类
在CustomAnnotationView.m文件里加载自定义气泡样式的CustomCalloutView,并且将数据传递给气泡CustomCalloutView上需要显示的label字符串。
创建自定义气泡View在 .h 文件中创建需要传值的字符串、图片和按钮
在.m文件里实现自定义View的样式
#import "CustomCalloutView.h"#define kArrorHeight 10#define kTitleWidth 200#define kTitleHeight 20 #define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]@interface CustomCalloutView()@property (nonatomic, strong) UILabel *subtitleLabel;@property (nonatomic, strong) UILabel *titleLabel;@property (nonatomic, strong) UIImageView *pointImge;@end@implementation CustomCalloutView//创建气泡背景样式- (void)drawRect:(CGRect)rect{[self drawInContext:UIGraphicsGetCurrentContext()];self.layer.shadowColor = [[UIColor blackColor] CGColor];self.layer.shadowOpacity = 0.2;self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);}- (void)drawInContext:(CGContextRef)context{CGContextSetLineWidth(context, 2.0);CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);[self getDrawPath:context];CGContextFillPath(context);}- (void)getDrawPath:(CGContextRef)context{CGRect rrect = self.bounds;CGFloat radius = 6.0;CGFloat minx = CGRectGetMinX(rrect),midx = CGRectGetMidX(rrect),maxx = CGRectGetMaxX(rrect);CGFloat miny = CGRectGetMinY(rrect),maxy = CGRectGetMaxY(rrect)-kArrorHeight;CGContextMoveToPoint(context, midx+kArrorHeight, maxy);CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);CGContextClosePath(context);}- (id)initWithFrame:(CGRect)frame{self = [super initWithFrame:frame];if (self){ self.backgroundColor = [UIColor clearColor]; [self initSubViews];}return self;}//自定义样式- (void)initSubViews{// 添加图片,即商户图self.pointImge = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 16, 18)];// self.pointImge.backgroundColor = COLOR(102, 102, 102, 1.0);[self addSubview:self.pointImge];// 添加标题,即商户名self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(35, 8, kTitleWidth, kTitleHeight)];self.titleLabel.font = [UIFont boldSystemFontOfSize:12];self.titleLabel.textColor = COLOR(102, 102, 102, 1.0);// self.titleLabel.text = @"titletitletitletitle";[self addSubview:self.titleLabel];// 添加副标题,即商户地址self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(35 ,5, kTitleWidth, kTitleHeight+30)];self.subtitleLabel.numberOfLines = 0;//表示label可以多行显示self.subtitleLabel.font = [UIFont boldSystemFontOfSize:12];self.subtitleLabel.textColor = COLOR(51, 51, 51, 1.0);// self.subtitleLabel.text = @"subtitleLabelsubtitleLabelsubtitleLabel";[self addSubview:self.subtitleLabel];//添加按钮self.button = [UIButton buttonWithType:UIButtonTypeCustom];self.button.frame = self.bounds;[self.button addTarget:self action:@selector(respondsToOnBtn) forControlEvents:UIControlEventTouchUpInside];[self addSubview:self.button];}- (void)setTitle:(NSString *)title{ self.titleLabel.text = title;}- (void)setSubtitle:(NSString *)subtitle{self.subtitleLabel.text = subtitle;}- (void)respondsToOnBtn{ NSLog(@"11111111111");}恩,写到这里代码基本上就算全部完成了。运行工程跑起来,也能看到默认的标注点了,but,气泡却没有自动弹出来,也响应不了按钮的点击事件。why? 然后就开始搜索技术贴,果然,有一些收获,有人说是因为本来气泡就写在了标注的视图上,层级关系不对,所以响应不到按钮,相对应的解决方法:在CustomAnnotationView.m文件中添加
- (UIView )hitTest:(CGPoint)point withEvent:(UIEvent )event解释一下这个方法的原理:iOS系统检测到手指触摸(Touch)操作时会将其放入当前活动Application的事件队列,UIApplication会从事件队列中取出触摸事件并传递给key window(当前接收用户事件的窗口)处理,window对象首先会使用hitTest:withEvent:方法寻找此次Touch操作初始点所在的视图(View),即需要将触摸事件传递给其处理的视图,称之为hit-test view。
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {UIView *view = [super hitTest:point withEvent:event];if (view == nil) { CGPoint tempoint = [self.calloutView.button convertPoint:point fromView:self]; if (CGRectContainsPoint(self.calloutView.button.bounds, tempoint)) { view = self.calloutView.button; }}return view;}这里设置了,按道理说,应该能够响应气泡层的按钮事件了。可是,地图上应该先弹出气泡,才能响应按钮事件。到这里,我又卡住了。然后直接在MAAnnotation代理中查看了相关代理方法。发现了有一个selected 的属性,瞬间欣喜如狂,在这下面加上了这个选中的属性。再次运行Demo.有一种感觉自己很聪明的傲娇。结果,地图上确实能够显示标注了,可是却点不动,而且点击气泡以外的View位置,居然气泡不消失了。what?这又是怎么回事…..
然后,在经历了多次测试后发现,气泡要默认弹出显示在地图上,是需要加载定位这个属性所调用的方法。在初始化加载地图的后面补上这段代码。
self.locationManager = [[AMapLocationManager alloc]init];//获取定位创建个人坐标self.locationManager.delegate = self;self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;//定位精确度在10米以内 //单次定位[self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) { if (location) { _pointAnnotaion = [[MAPointAnnotation alloc]init]; _pointAnnotaion.title = @"蓝鲸鱼待久大酒店蓝鲸鱼待久大酒店蓝鲸鱼待久大酒店"; CLLocationCoordinate2D coordinate = {30.6594620000,104.0657350000}; _mapView.centerCoordinate = coordinate; _pointAnnotaion.coordinate = coordinate; _mapView.zoomLevel = 17; [self.mapView addAnnotation:self.pointAnnotaion]; //默认选中标注数据对应的view (同时在地图 MAMapViewDelegate方法中,去掉selected这个选中) [self.mapView selectAnnotation:self.pointAnnotaion animated:YES]; }}];然后就可以完全实现自己想要的效果了。
注:以上均为个人的实现方法和建议,如果有更好的方法,欢迎回复讨论。