引入framework 实现下面这个代理方法
// Override
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
if ([annotation isKindOfClass:[MAPointAnnotation class]])
{
static NSString *reuseIndetifier = @"annotationReuseIndetifier";
PointAnnotationView *annotationView = (PointAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
if (annotationView == nil)
{
annotationView = [[PointAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
}
annotationView.image = [UIImage imageNamed:@"targetPoint"];
NSString * result = [[annotation title] stringByReplacingOccurrencesOfString:@"点标" withString:@""]; //截取字符串
result = [result stringByReplacingOccurrencesOfString:@"点" withString:@""];
annotationView.point_ID.text = [result stringByReplacingOccurrencesOfString:@"号" withString:@""];
// annotationView.point_url = self.gameInfo.img_url;
//设置中心点偏移,使得标注底部中间点成为经纬度对应点
annotationView.centerOffset = CGPointMake(0, -18);
annotationView.canShowCallout = NO;
return annotationView;
}
return nil;
}
新建PointAnnotationView继承自MAAnnotationView
#import <MAMapKit/MAMapKit.h>
#import "PointView.h"
@interface PointAnnotationView : MAAnnotationView
@property (nonatomic, readonly) PointView *point_View;
@property (nonatomic, strong) UILabel *point_ID;
//@property (nonatomic, strong) NSString *point_url;
@end
#import "PointAnnotationView.h"
#define kCalloutWidth 80.0
#define kCalloutHeight 50.0
@interface PointAnnotationView ()
@property (nonatomic, strong, readwrite) PointView *point_View;
@end
@implementation PointAnnotationView
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if (self.selected == selected)
{
return;
}
if (selected)
{
if (self.point_View == nil)
{
self.point_View = [[PointView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
self.point_View.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
-CGRectGetHeight(self.point_View.bounds) / 2.f + self.calloutOffset.y);
}
// UIImageView *tempImageView = [[UIImageView alloc] init];
// [tempImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",hostUrl,self.point_url]] placeholderImage:[UIImage imageNamed:@"加载中"] options:SDWebImageRefreshCached];
// self.point_View.image = tempImageView.image;
self.point_View.title = self.annotation.title;
self.point_View.subtitle = self.annotation.subtitle;
[self addSubview:self.point_View];
}
else
{
[self.point_View removeFromSuperview];
}
[super setSelected:selected animated:animated];
}
-(instancetype)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
if ([super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier]) {
//在大头针旁边加一个label
self.point_ID = [[UILabel alloc] initWithFrame:CGRectMake(5, -12, 20, 60)];
self.point_ID.textAlignment = NSTextAlignmentCenter;
self.point_ID.backgroundColor = [UIColor clearColor];
self.point_ID.font = [UIFont fontWithName:@"Helvetica-Bold" size:10];
self.point_ID.textColor = QDXBlack;
[self addSubview:self.point_ID];
}
return self;
}
@end
点击后气泡框
#import <UIKit/UIKit.h>
@interface PointView : UIView
@property (nonatomic, strong) UIImage *image; //商户图
@property (nonatomic, copy) NSString *title; //商户名
@property (nonatomic, copy) NSString *subtitle; //地址
@end
#import "PointView.h"
#define kArrorHeight 10
#define kPortraitMargin 10
#define kPortraitWidth 70
#define kPortraitHeight 50
#define kTitleWidth 150
#define kTitleHeight 20
@interface PointView ()
@property (nonatomic, strong) UIImageView *portraitView;
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@end
@implementation PointView
- (void)drawRect:(CGRect)rect
{
[self drawInContext:UIGraphicsGetCurrentContext()];
// self.layer.shadowColor = [[UIColor whiteColor] CGColor];
// self.layer.shadowOpacity = 1.0;
// self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}
- (void)drawInContext:(CGContextRef)context
{
CGContextSetLineWidth(context, 2.0);
CGContextSetFillColorWithColor(context, [UIColor colorWithWhite:1.000 alpha:1.0].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.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(kPortraitMargin, kPortraitMargin, kPortraitWidth, kPortraitHeight)];
self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
self.portraitView.backgroundColor = [UIColor blackColor];
[self addSubview:self.portraitView];
// 添加标题,即商户名
// self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin, kTitleWidth, kTitleHeight)];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin, kPortraitMargin, kTitleWidth, kTitleHeight)];
self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
self.titleLabel.textColor = QDXBlack;
self.titleLabel.text = @"title";
[self addSubview:self.titleLabel];
// 添加副标题,即商户地址
// self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin * 2 + kTitleHeight, kTitleWidth, kTitleHeight)];
self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin , kPortraitMargin * 2 + kTitleHeight, kTitleWidth, kTitleHeight)];
self.subtitleLabel.font = [UIFont systemFontOfSize:12];
self.subtitleLabel.textColor = QDXGray;
self.subtitleLabel.text = @"subtitleLabel";
[self addSubview:self.subtitleLabel];
}
- (void)setTitle:(NSString *)title
{
self.titleLabel.text = title;
}
- (void)setSubtitle:(NSString *)subtitle
{
self.subtitleLabel.text = subtitle;
}
- (void)setImage:(UIImage *)image
{
self.portraitView.image = image;
}