自定义地图(高德)AnnotationView的一些总结

高德地图API点击气泡效果是一个白色的对话框,像这样的. 当大头针在屏幕边缘的时候,弹出的对话框是默认可见范围的,也就是说那个对话框不是在大头针的center上方出现,所以体验不好,而且只能显示titile 和 sub两句话.

自定义这个类必须继承与MAAnnotationView.

下面上代码:

这是选中非选中的逻辑代码实现

- (void)setSelected:(BOOL)selected

{

[self setSelected:selected animated:NO];

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{

if (self.selected == selected)

{

return;

}

if (selected)

{

if (self.calloutView == nil)

{

/* Construct custom callout. */

self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];

self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,

-CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);

UILabel * nameLabel  =[[UILabel alloc]initWithFrame:CGRectMake(3, 0 , kCalloutWidth - 3, 20)];

UILabel * timeLabel  =[[UILabel alloc]initWithFrame:CGRectMake(3, 20 , kCalloutWidth - 3, 20)];

UILabel * waterLabel =[[UILabel alloc]initWithFrame:CGRectMake(3, 40 , kCalloutWidth - 3, 20)];

UILabel * QLabel    =[[UILabel alloc]initWithFrame:CGRectMake(3, 60 , kCalloutWidth - 3, 20)];

UILabel * numLabel  =[[UILabel alloc]initWithFrame:CGRectMake(kCalloutWidth - 103,0, 100, 20)];

[self setLabelType:nameLabel AndLabel2:timeLabel AndLabel3:waterLabel AndLabel4:QLabel AndLabel5:numLabel];

}

[self addSubview:self.calloutView];

}else

{

[self.calloutView removeFromSuperview];

}

[super setSelected:selected animated:animated];

}

至于其中的CustomCalloutView是我自己画的一个对话框类型的View,效果如下:



这样就满足了View是在大头针的正上方出现的,而且数据也可以自定义显示出来.



下面介绍这个类型对话框的View的画法,其实可以让美工直接给图的,不过因为当时需求特殊,我就自己画了一个.下面上代码:

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

@implementation CustomCalloutView

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

self.backgroundColor = [UIColor clearColor];

}

return self;

}

#pragma mark - draw rect

- (void)drawRect:(CGRect)rect

{

[self drawInContext:UIGraphicsGetCurrentContext()];

self.layer.shadowColor = [[UIColor blackColor] 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 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, maxy, radius);

CGContextAddArcToPoint(context, maxx, maxy, midx + kArrorHeight, maxy, radius);

CGContextClosePath(context);

}

无非是用了CGContext这个函数,在View上的画布,自己根据需求点,描绘图形

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • //设置尺寸为屏幕尺寸的时候self.window = [[UIWindow alloc] initWithFra...
    LuckTime阅读 4,280评论 0 0
  • 直入正题吧! 实现三个代理方法: 这个方法类似tableview添加cell,都是创建annotation #pr...
    CS二哥阅读 5,697评论 5 5
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 5,313评论 9 5
  • VLC的集成和使用 VLC介绍 VLC Media Player (VideoLAN) 为 Windows、Lin...
    Pocket阅读 20,045评论 75 66
  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 10,215评论 1 9