高德地图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上的画布,自己根据需求点,描绘图形