百度地图知识积累:
1.关于大头针自定义
首先理解 Annotation、BMKAnnotationView
如果要携带更多的数据,就需要自定义Annotation
@interface BasicMapAnnotation:NSObject<BMKAnnotation>
//BMKAnnotation 协议规定必须实现的
@property (nonatomic,assign) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *subtitle;
//自己需要携带的数据
@property (nonatomic,strong) NSMutableArray *animatedImages;
@property (nonatomic,strong) NSArray *listArray;
- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;
BMKAnnotationView :在地图上看到的大头针view,点击产生的气泡:系统默认、自己定义
自己定义的气泡:
@interface CustomAnnotationView :BMKPinAnnotationView
@property (nonatomic,strong)NSString *title;
@property (nonatomic,strong)UIImage *avatarImage;
@property (nonatomic,strong)CustomCalloutView *calloutView; //泡泡
@end
.m实现中
#import "CustomAnnotationView.h"
#import "CustomCalloutView.h"
#define kWidth 150.f
#define kHeight 60.f
#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 CustomAnnotationView ()
@property (nonatomic, strong) UIImageView *portraitImageView;
@property (nonatomic, strong) UILabel *nameLabel;
@end
@implementation CustomAnnotationView
- (void)setTitle:(NSString *)title
{
_nameLabel.text = title;
}
- (NSString *)title
{
return _nameLabel.text;
}
- (void)setAvatarImage:(UIImage *)avatarImage
{
_portraitImageView.image = avatarImage;
self.portraitImageView.image = avatarImage;
}
- (UIImage *)avatarImage
{
return _portraitImageView.image;
}
#pragma mark - Life Cycle
- (id)initWithAnnotation:(id <BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier;
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
self.image = nil;
if (self)
{
//这里进行布局
self.bounds = CGRectMake(0.f, 0.f, kWidth, kHeight);
self.backgroundColor = [UIColor grayColor];
/* Create portrait image view and add to view hierarchy. */
self.portraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kHoriMargin, kVertMargin, kPortraitWidth, kPortraitHeight)];
[self addSubview:self.portraitImageView];
/* Create name label. */
self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitWidth + kHoriMargin,
kVertMargin,
kWidth - kPortraitWidth - kHoriMargin,
kHeight - 2 * kVertMargin)];
self.nameLabel.backgroundColor = [UIColor clearColor];
self.nameLabel.textAlignment = NSTextAlignmentCenter;
self.nameLabel.textColor = [UIColor whiteColor];
self.nameLabel.font = [UIFont systemFontOfSize:15.f];
[self addSubview:self.nameLabel];
}
return self;
}
- (void)setSelected:(BOOL)selected
{
[self setSelected:selected animated:NO];
}
<strong><span style="color:#009900;">//气泡的点击出现,将气泡view加在了大头针view上面</span></strong>
- (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);
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(10, 10, 40, 40);
[btn setTitle:@"Test" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[btn setBackgroundColor:[UIColor whiteColor]];
[btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
[self.calloutView addSubview:btn];
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, 100, 30)];
name.backgroundColor = [UIColor clearColor];
name.textColor = [UIColor whiteColor];
name.text = @"Hello Amap!";
[self.calloutView addSubview:name];
}
[self addSubview:self.calloutView];
}
else
{
[self.calloutView removeFromSuperview];
}
[super setSelected:selected animated:animated];
}
#import "CustomCalloutView.h"
#import <QuartzCore/QuartzCore.h>
#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 colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.8].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);
}
第二种:已经添加在了BMKpinannotainView上面,先是隐藏,当点击大头针时,再控制显示
#import "CallOutAnnotationVifew.h"
#import <QuartzCore/QuartzCore.h>
#define Arror_height 15
@interface CallOutAnnotationVifew ()
-(void)drawInContext:(CGContextRef)context;
- (void)getDrawPath:(CGContextRef)context;
@end
@implementation CallOutAnnotationVifew
@synthesize contentView;
- (void)dealloc
{
self.contentView = nil;
[super dealloc];
}
- (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.canShowCallout = NO;
self.centerOffset = CGPointMake(0, -55);
self.frame = CGRectMake(0, 0, 240, 80);
UIView *_contentView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - Arror_height)] autorelease];
_contentView.backgroundColor = [UIColor clearColor];
[self addSubview:_contentView];
self.contentView = _contentView;
}
return self;
}
-(void)drawInContext:(CGContextRef)context
{
CGContextSetLineWidth(context, 2.0);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
[self getDrawPath:context];
CGContextFillPath(context);
// CGContextSetLineWidth(context, 1.0);
// CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
// [self getDrawPath:context];
// CGContextStrokePath(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),
// midy = CGRectGetMidY(rrect),
maxy = CGRectGetMaxY(rrect)-Arror_height;
CGContextMoveToPoint(context, midx+Arror_height, maxy);
CGContextAddLineToPoint(context,midx, maxy+Arror_height);
CGContextAddLineToPoint(context,midx-Arror_height, 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);
}
- (void)drawRect:(CGRect)rect
{
[self drawInContext:UIGraphicsGetCurrentContext()];
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowOpacity = 1.0;
// self.layer.shadowOffset = CGSizeMake(-5.0f, 5.0f);
self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}
@end
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
if ([annotation isKindOfClass:[CalloutMapAnnotation class]])
{
CallOutAnnotationVifew *annotationView = (CallOutAnnotationVifew *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CalloutView"];
if (!annotationView)
{
annotationView = [[[CallOutAnnotationVifew alloc] initWithAnnotation:annotation reuseIdentifier:@"CalloutView"] autorelease];
JingDianMapCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"JingDianMapCell" owner:self options:nil] objectAtIndex:0];
[annotationView.contentView addSubview:cell];
}
return annotationView;
}
return nil;
}
//点击了大头针
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
{
if ([view isKindOfClass:[CallOutAnnotationVifew class]]) { | |
[((DXAnnotationView *)view) showCalloutView]; | |
} |
}