地图大头针和气泡callout的自定制,以及气泡的点击事件

要达到的效果图

Snip20170727_7.png

一.首先看下地图中最基础的大头针的定制:

#import <MAMapKit/MAMapKit.h>
#import "CustomCalloutView.h"//自定制气泡callout
#import "LLAllCarModel.h" //处理数据的model
@interface CustomAnnotationView : MAAnnotationView
@property (nonatomic, readonly) CustomCalloutView *calloutView;
@property (nonatomic, strong) LLAllCarModel  *mod;

//这两个block就是实现点击callout上面的按钮实现相应的点击事件(原理就是将CustomAnnotationView上的两个button返回到主ViewController中)
@property (nonatomic, copy) void(^sumButtonClickBlock)(CustomAnnotationView *annoView);
@property (nonatomic, copy) void(^historyButtonClickBlock)(CustomAnnotationView *annoView);

- (void)refreshAnnotationData:(LLAllCarModel *)model;

@end





#import "CustomAnnotationView.h"
#define kCalloutWidth       130.0
#define kCalloutHeight      210.0
@interface CustomAnnotationView()
@property (nonatomic, strong, readwrite) CustomCalloutView *calloutView;

@end

@implementation CustomAnnotationView

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    if (self.selected == selected) {
        return;
    }
    if (selected) {
        if (self.calloutView == nil) {
            self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
            _calloutView.userInteractionEnabled = YES;
            self.calloutOffset = CGPointMake(0, -66);
            self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.0 + self.calloutOffset.x, -CGRectGetHeight(self.bounds) / 2.0 + self.calloutOffset.y);
        }
        //callout按钮的点击事件
            [self.calloutView.sumBtn addTarget:self action:@selector(onSumBtnClick) forControlEvents:UIControlEventTouchUpInside];
            [self.calloutView.historyBtn addTarget:self action:@selector(onHistoryBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self.calloutView refreshnewData:_mod];
        
        [self addSubview:_calloutView];
    }else{
        [self.calloutView removeFromSuperview];
    }
    [super setSelected:selected animated:animated];
    
}

- (void)refreshAnnotationData:(LLAllCarModel *)model{
    _mod = model;
    
}
//将单击事件回传到viewcontroller中
- (void)onSumBtnClick {
    if(self.sumButtonClickBlock) {
        self.sumButtonClickBlock(self);
    }
}
//将单击事件回传到viewcontroller中
- (void)onHistoryBtnClick {
    if(self.historyButtonClickBlock) {
        self.historyButtonClickBlock(self);
    }
}
//重写hitTest方法是点击callOut上的按钮有效
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    UIView *view = [super hitTest:point withEvent:event];
    [view addSubview:_sumButton];
    [view addSubview:_historyButton];
    if (view == nil) {
        CGPoint sumPoint = [self.calloutView.sumBtn convertPoint:point fromView:self];
        if (CGRectContainsPoint(self.calloutView.sumBtn.bounds, sumPoint)) {
            view = self.calloutView.sumBtn;
            NSLog(@"11111111111111111");
            return view;
            
        }
        CGPoint hisPoint = [self.calloutView.historyBtn convertPoint:point fromView:self];
        if (CGRectContainsPoint(self.calloutView.historyBtn.bounds, hisPoint)) {
            view = self.calloutView.historyBtn;
            NSLog(@"22222222222222222222");
            return view;
        }
    }
    return view;

}

二.看下气泡callout的自定制

#import <UIKit/UIKit.h>
#import "LLAllCarModel.h"

@interface CustomCalloutView : UIView

@property (nonatomic, strong) UILabel *imeiL;
@property (nonatomic, strong) UILabel *statusL;
@property (nonatomic, strong) UILabel *temperL;
@property (nonatomic, strong) UIImageView *gpsL;
@property (nonatomic, strong) UIImageView *gsmL;

@property (nonatomic, strong) UIButton *sumBtn;//里程统计
@property (nonatomic, strong) UIButton *historyBtn;//历史轨迹
//这两个block就是实现点击callout上面的按钮实现相应的点击事件(原理就是将CustomAnnotationView上的两个button返回到主ViewController中)
@property (nonatomic, copy) void(^sumButtonClickBlock)(CustomAnnotationView *annoView);
@property (nonatomic, copy) void(^historyButtonClickBlock)(CustomAnnotationView *annoView);

- (void)refreshnewData:(LLAllCarModel *)model;

@end



#import "CustomCalloutView.h"
#define kArrorHeight        10
@implementation CustomCalloutView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor clearColor];
        [self createSubView];
    }
    return self;
}

- (void)createSubView{
    CGFloat gap = 10;
    NSArray *nameLabelArr = @[@"IMEI:",@"状态:",@"温度:",@"GPS:",@"GSM:"];
    for (int i = 0; i < nameLabelArr.count; i ++) {
        UILabel *namesL = [FactoryUI createLabelWithFrame:CGRectMake(gap, gap + 20*i + gap*i, 40, 20) title:nameLabelArr[i] andFont:13];
        [self addSubview:namesL];
        UILabel *subL = [FactoryUI createLabelWithFrame:CGRectMake(CGRectGetMaxX(namesL.frame)+5, CGRectGetMinY(namesL.frame), 70, 20) title:nil andFont:13];
        if (i == 0) {
            _imeiL = subL;
            [self addSubview:_imeiL];
        }else if (i == 1) {
            _statusL = subL;
            [self addSubview:_statusL];
        }else if (i == 2) {
            _temperL = subL;
            [self addSubview:_temperL];
        }else if (i == 3) {
            _gpsL = [FactoryUI createImageViewWithFrame:CGRectMake(CGRectGetMaxX(namesL.frame)+5, CGRectGetMinY(namesL.frame), 20, 20) andImageName:nil];
            [self addSubview:_gpsL];
        }else if (i == 4) {
            _gsmL = [FactoryUI createImageViewWithFrame:CGRectMake(CGRectGetMaxX(namesL.frame)+5, CGRectGetMinY(namesL.frame), 20, 20) andImageName:nil];;
            [self addSubview:_gsmL];
        }
    }
    UILabel *horizentalLine = [FactoryUI createLabelWithFrame:CGRectMake(0, 155, self.bounds.size.width, 0.4) title:nil andFont:1];
    horizentalLine.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:horizentalLine];
    
    _sumBtn = [FactoryUI createButtonWithFrame:CGRectMake(0, CGRectGetMaxY(horizentalLine.frame), self.bounds.size.width / 2.0, self.bounds.size.height - CGRectGetMaxY(horizentalLine.frame)-10) title:@"里程" normalImage:nil selectedImage:nil titleColor:[UIColor colorWithHexString:@"#1989fa"] target:nil selector:nil andTag:12];
    _sumBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self addSubview:_sumBtn];
    UILabel *versialLine = [FactoryUI createLabelWithFrame:CGRectMake(self.bounds.size.width/2.0-1, CGRectGetMaxY(horizentalLine.frame), 0.4, CGRectGetHeight(_sumBtn.frame) - 10) title:nil andFont:1];
    versialLine.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:versialLine];
    _historyBtn = [FactoryUI createButtonWithFrame:CGRectMake(CGRectGetMaxX(_sumBtn.frame), CGRectGetMinY(_sumBtn.frame), self.bounds.size.width / 2.0, CGRectGetHeight(_sumBtn.frame)) title:@"轨迹" normalImage:nil selectedImage:nil titleColor:[UIColor colorWithHexString:@"#1989fa"] target:nil selector:nil andTag:21];
    _historyBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self addSubview:_historyBtn];
}
//刷新callout上面的数据
- (void)refreshnewData:(LLAllCarModel *)model{
    _imeiL.text = model.name;
    if ([model.status isEqualToString:@"M"]) {
        _statusL.textColor = [UIColor redColor];
        _statusL.text = @"故障";
    }else if ([model.status isEqualToString:@"O"]){
        _statusL.textColor = [UIColor redColor];
        _statusL.text = @"断开";
    }else if ([model.status isEqualToString:@"N"] || [model.status isEqualToString:@"S"]){
        _statusL.textColor = [UIColor greenColor];
        _statusL.text = @"正常";
    }
    
    _temperL.text = model.temper;
    [_gpsL sd_setImageWithURL:[NSURL URLWithString: model.gps]];
    [_gsmL sd_setImageWithURL:[NSURL URLWithString:  model.gsm]];
}

//***************下面👇这里及时绘制callout的方法***************************
//1.这个是callout背景颜色
- (void)drawRect:(CGRect)rect{
    [self drawInContext:UIGraphicsGetCurrentContext()];
    self.layer.shadowColor = [UIColor whiteColor].CGColor;
    self.layer.shadowOpacity = 0.7;
    self.layer.shadowOffset = CGSizeMake(3.0f, 3.0f);
    
}
//callout填充
- (void)drawInContext:(CGContextRef)context{
    CGContextSetLineWidth(context, 2.0);
    CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:1.0 alpha:0.8].CGColor);
    
    [self getDrawPath:context];
    CGContextFillPath(context);
}
//划线路径
- (void)getDrawPath:(CGContextRef)context{
    CGRect rrect = self.bounds;
    CGFloat radius = 6;
    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);
}

最后在Viewcontroll中显示自定制的大头针和callout

#pragma mark - MAMapViewDelegate
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation{
    if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
        //标注view 的初始化和复用
        static NSString *reuseString = @"allCatAnnotation";
        CustomAnnotationView *cusAnnotation = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseString];
        if (!cusAnnotation) {
            cusAnnotation = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseString];
        }
        for (LLAllCarModel *model in self.allCarArr) {
            //判断是哪个大头针
            if ([model.lat floatValue] == annotation.coordinate.latitude && [model.lon floatValue] == annotation.coordinate.longitude) {
                
                [cusAnnotation refreshAnnotationData:model];
                
//*********************这里是我要监控CustomAnnotationView里面sumButton和historyButton的单击事件的*******************
               cusAnnotation.sumButtonClickBlock = ^(CustomAnnotationView *annoView) {
                    NSLog(@"点击了model.imei====%@的里程统计",model.imei);
                    LLMSumViewController *sumVC = [[LLMSumViewController alloc] init];
                    sumVC.imeiStr = model.imei;
                    sumVC.nickNameStr = model.name;
                    [self.navigationController pushViewController:sumVC animated:YES];
                };
                cusAnnotation.historyButtonClickBlock = ^(CustomAnnotationView *annoView) {
                    NSLog(@"点击了model.imei====%@的历史轨迹",model.imei);
                    LLhistoryViewController *historyVC = [[LLhistoryViewController alloc] init];
                    historyVC.imeiStr = model.imei;
                    historyVC.nickNameStr = model.name;
                    [self.navigationController pushViewController:historyVC animated:YES];
                };

//************************************************************
            }
        }
        
        
        cusAnnotation.draggable = NO;
        cusAnnotation.canShowCallout = NO;//将自带的关掉
        cusAnnotation.image = [UIImage imageNamed:@"icon_position"];;
        cusAnnotation.centerOffset = CGPointMake(0, -33);
        
        return cusAnnotation;
    }
    return nil;

    
}

到此.气泡的点击事件就可以实现了.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容

  • 当高德地图自带的大头针气泡不能满足项目需求时,需要开发者自定义地图大头针气泡。比如气泡上显示个图片,标题或者其他之...
    追沐阅读 8,149评论 12 8
  • 根据项目需求,需要自定制起点、途经点以及终点的大头针,下面看一下怎么去创建? 1.自定义MyPointAnnota...
    plu阅读 2,650评论 3 2
  • 出自http://my.oschina.net/are1OfBlog/blog/420034 摘要 现在很多社交、...
    JJO阅读 4,094评论 4 19
  • 上午的情绪不好,一度憋得心口儿生疼,这成了我的情绪压抑时的一个典型症状,一旦有压抑,必疼无疑。不过,它倒有一个好处...
    铱漩娜阅读 89评论 0 0
  • 人生正如微信朋友圈,你永远不知道下一个做微商的是谁。 是的,我要告诉你们的是我也开始了微商。请先不要退出来也不要急...
    紫竹筱琴阅读 1,214评论 2 1