高德地图 自定义大头针

#import <MAMapKit/MAMapKit.h>

#import "MLFoundCarModel.h"

@protocol MLCustomerAnnotationviewDelegate<NSObject>

-(void)clickAnnotionView:(NSString * )ID subtitle:(NSString *)subtitle;//点击大头针  操作

@end

@interface MLCustomAnnotationView : MAAnnotationView

@property (nonatomic, copy) NSString *name;

@property (nonatomic, strong) UIImage *portrait;

@property (nonatomic, strong) UIView *calloutView;

@property (nonatomic,weak)id<MLCustomerAnnotationviewDelegate>delegate;

@property (nonatomic, strong) UIView *titleView;

@property (nonatomic, strong) UILabel *titles;

@property (nonatomic, strong) NSMutableArray *countArray;

@property (nonatomic, strong) NSMutableArray *datasArr;

#import "MLCustomAnnotationView.h"

#define kWidth  84/2

#define kHeight 53+15

#define kHoriMargin 5.f

#define kVertMargin 5.f

#define kPortraitWidth  50.f

#define kPortraitHeight 50.f

#define kCalloutWidth   200.0

#define kCalloutHeight  70.0

@interface MLCustomAnnotationView ()

@property (nonatomic, strong) UIImageView *portraitImageView;

@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation MLCustomAnnotationView

@synthesize calloutView;

@synthesize portraitImageView   = _portraitImageView;

@synthesize nameLabel           = _nameLabel;

#pragma mark - Handle Action

- (void)btnAction

{

    CLLocationCoordinate2D coorinate = [self.annotation coordinate];

    

    NSLog(@"coordinate = {%f, %f}", coorinate.latitude, coorinate.longitude);

}

#pragma mark - Override

- (NSString *)name

{

    return self.nameLabel.text;

}

- (void)setName:(NSString *)name

{

    self.nameLabel.text = name;

}

- (UIImage *)portrait

{

    return self.portraitImageView.image;

}

- (void)setPortrait:(UIImage *)portrait

{

    self.portraitImageView.image = portrait;

}

- (void)setSelected:(BOOL)selected

{

    [self setSelected:selected animated:NO];

}

-(void)setCountArray:(NSMutableArray *)countArray{

    _countArray = countArray;

    

}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event

{

    BOOL inside = [super pointInside:point withEvent:event];

    /* Points that lie outside the receiver’s bounds are never reported as hits,

     even if they actually lie within one of the receiver’s subviews.

     This can occur if the current view’s clipsToBounds property is set to NO and the affected subview extends beyond the view’s bounds.

     */

    if (!inside && self.selected)

    {

        inside = [self.calloutView pointInside:[self convertPoint:point toView:self.calloutView] withEvent:event];

    }

    

    return inside;

}

#pragma mark - Life Cycle

- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

    

    if (self)

    {

        self.bounds = CGRectMake(0.f, 0.f, kWidth, kHeight);

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickanimoter:)];

        tap.view.tag = self.zIndex;

        [self addGestureRecognizer:tap];

        self.backgroundColor = [UIColor clearColor];

        [self setlayout];

        NSLog(@"sub == %@",self.annotation.subtitle);

    }

    

    return self;

}

-(void)clickanimoter:(UITapGestureRecognizer *)sender{

    if(_delegate && [_delegate respondsToSelector:@selector(clickAnnotionView:subtitle:)]){

        [_delegate clickAnnotionView:self.annotation.title subtitle:self.annotation.subtitle];

    }

}

#pragma mark 布局

-(void)setlayout{

    WS(weakself);

    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.offset(0);

        make.centerX.mas_equalTo(weakself.mas_centerX);

        make.size.mas_equalTo(CGSizeMake(36, 15));

    }];

    [self.titles mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.right.top.bottom.offset(0);

    }];

    [self.portraitImageView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.centerX.mas_equalTo(weakself.mas_centerX);

        make.top.equalTo(weakself.titleView.mas_bottom).offset(3);

        make.size.mas_equalTo(CGSizeMake(84/2, 105/2));

        make.bottom.offset(0);

    }];

}

#pragma mark  懒加载

-(UIView *)titleView{

    if(!_titleView){

        _titleView = [[UIView alloc]init];

        _titleView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4f];

        _titleView.layer.cornerRadius = 8.0f;

        _titleView.layer.masksToBounds = YES;

        [self addSubview:self.titleView];

    }

    return _titleView;

}

-(UILabel *)titles{

    if(!_titles){

        _titles = [UILabel labelWithText:@"" atColor:White_Color atTextSize:12 atTextFontForType:Common_Font];

        _titles.textAlignment = NSTextAlignmentCenter;

        _titles.font = [UIFont fontWithName:@"Arial" size:11];

        [self.titleView addSubview:self.titles];

    }

    return _titles;

}

-(UIImageView *)portraitImageView{

    if(!_portraitImageView){

        self.portraitImageView = [UIImageView imageViewWithImageName:@""];

        self.portraitImageView.userInteractionEnabled = YES;

         [self addSubview:self.portraitImageView];

    }

    return _portraitImageView;

}

原文地址:https://www.cnblogs.com/liaolijun/p/7797768.html