百度地图自定义吹出框

直入正题吧!

这些都是知道的了,看文档添加就行了!

实现三个代理方法:

这个方法类似tableview添加cell,都是创建annotation

#pragma mark #pragma mark - BMKMapview delegate -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation;

这个方法在点击地图marker时所触发(并显示callout)

-(void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view;

这个方法在点击地图任意位置,相当于隐藏callout

-(void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view;

原理:地图上的marker是在viewForAnnoation里创建的,同时也会 隐含的为我们创建一个CalloutView,就是自带的吹出框,只是我们看不到源码。其实这个吹出框(CalloutView)也是一个 annotation,也会在viewForAnnotation里被创建,他的坐标应该和这个点的marker坐标一样,只要明白了这一点,就行了,marker和吹出框是两个不同的annotation,他们有同样的coordinate

第一步:

自定义一个Annotation,为了简单方便,我就直接继承了mapview自带的BMKPointAnnotation,这是一个经典的图钉marker。

以下部分是为自定义annotation添加显示更多信息

第二步:

创建一个(自定义的CalloutView)的Annotation,相当于显示calloutView的annotation。

[注意] 继承自NSObject<BMKAnnotation>

第三步:

这一步我们创建自定义的View,想要什么布局就写什么样的布局,想要多少属性就加多少属性。

[注意:继承自BMKAnnotationView]

AnnotationPointCell是ContentView里的subview,这个view就是显示各个组件,并赋不同的值

CallOutAnnotationView.m

#import "CallOutAnnotationView.h"

#define Arror_height 6

@implementation CallOutAnnotationView

- (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];

if (self) {

}

return self;

}

-(id)initWithAnnotation:(id)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(5, 5, self.frame.size.width-15, self.frame.size.height-15)];

contentView.backgroundColor = [UIColor clearColor];

[self addSubview:contentView];

self.contentView = contentView;

} return self;

}

-(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:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 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),

// 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);

}

AnnotationPointCell里面想添加什么内容就自己定吧

第四步:

下面是VC里面的主要代码

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation

{

static NSString * annotationIndentifier = @"customAnnotation";

if ([annotation isKindOfClass:[CusTomPointAnnotation class]]) {

BMKPinAnnotationView * annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIndentifier];

/**

设置大头针图片

annotationView.image = [UIImage imageNamed:@""];

**/

[annotationView setAnimatesDrop:YES];

//[注意]在添加marker的判断里一定要设置markerannotation.canShowCallout =NO; 否则点击marker会默认显示系统的吹出框

annotationView.canShowCallout = NO;

return annotationView;

}else if ([annotation isKindOfClass:[CalloutMapAnnotation class]]){

//此时annotation就是我们calloutview的annotation

CalloutMapAnnotation *ann = (CalloutMapAnnotation*)annotation;

//如果可以重用

CallOutAnnotationView *calloutannotationview = (CallOutAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"calloutview"];

//否则创建新的calloutView

if (!calloutannotationview) {

calloutannotationview = [[CallOutAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"calloutview"];

AnnotationPointCell *cell = [[AnnotationPointCell alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];

[calloutannotationview.contentView addSubview:cell];

calloutannotationview.busInfoView = cell;

}

//开始设置添加marker时的赋值

calloutannotationview.busInfoView.nameLabel.text = [ann.locationInfo objectForKey:@"alias"];

calloutannotationview.busInfoView.contentLabel.text = [ann.locationInfo objectForKey:@"speed"];

calloutannotationview.busInfoView.timeLabel.text =[ann.locationInfo objectForKey:@"degree"];

//        calloutannotationview.busInfoView.titleImageView.image = [UIImage imageNamed:@""];

return calloutannotationview;

}

return nil;

}

- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view

{

//CustomPointAnnotation 是自定义的marker标注点,通过这个来得到添加marker时设置的pointCalloutInfo属性

CusTomPointAnnotation *annn = (CusTomPointAnnotation*)view.annotation;

if ([view.annotation isKindOfClass:[CusTomPointAnnotation class]]) {

//如果点到了这个marker点,什么也不做

if (_calloutMapAnnotation.coordinate.latitude == view.annotation.coordinate.latitude && _calloutMapAnnotation.coordinate.longitude == view.annotation.coordinate.longitude) {

return;

} //如果当前显示着calloutview,又触发了select方法,删除这个calloutview annotation(处理多个气泡的情况)

if (_calloutMapAnnotation) {

[mapView removeAnnotation:_calloutMapAnnotation];

_calloutMapAnnotation=nil;

} //创建搭载自定义calloutview的annotation

_calloutMapAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude andLongitude:view.annotation.coordinate.longitude];

//把通过marker(ZNBCPointAnnotation)设置的pointCalloutInfo信息赋值给CalloutMapAnnotation

_calloutMapAnnotation.locationInfo = annn.pointCalloutInfo;

[mapView addAnnotation:_calloutMapAnnotation];

[mapView setCenterCoordinate:view.annotation.coordinate animated:YES];

}else if ([view isKindOfClass:[CallOutAnnotationView class]]) {

//这里处理点击自定义吹出框的事件

}

}

#pragma mark - 这个方法在点击地图任意位置,相当于隐藏callout

//这个方法在点击地图任意位置,相当于隐藏callout

-(void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view{

if (_calloutMapAnnotation&&![view isKindOfClass:[CallOutAnnotationView class]]&&![view isKindOfClass:[CalloutMapAnnotation class]]) {

if (_calloutMapAnnotation.coordinate.latitude == view.annotation.coordinate.latitude) {

[mapView removeAnnotation:_calloutMapAnnotation];

_calloutMapAnnotation = nil;

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,319评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,801评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,567评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,156评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,019评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,090评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,500评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,192评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,474评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,566评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,338评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,212评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,572评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,890评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,169评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,478评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,661评论 2 335

推荐阅读更多精彩内容