IOS百度地图BMKAnnotationView添加子视图,子视图点击事件不响应解决办法

做开发有一段时间了,第一次写文章,主要记录下使用百度地图中遇到的比较棘手的问题以及解决办法,直接进入主题吧,最近项目中遇到一个需求,在地图zoomlevel较小的时候地图标注只显示一个图片,此时图片可以点击,然后显示详情view(数据较复杂),详情view也有点击事件,也可以点击,然后弹出另一个controller,效果大致如下:

默认显示样式


点击显示自定义view



点击弹出view在弹出个controller

在地图zoomlevel被放大到一定程度后,不需要点击,自动显示所有的自定义view,效果如图:


zoomlevel变大自动显示详情view


点击view弹出controller



笔者主要记录如何解决这个需求,如何配置地图以及定位等基础功能详情请参考官方文档或者我给出的Demo都有详细的注释,先说下我的解决思路吧,首先拿到数据后,在地图上将点都描出来(自定义view的数据信息是小图标的详情,所以经纬度是一样的),注意是把小图标和自定义view都作为标注点描出来,通过调节centeroffset,frame等属性使得两个标注的位置如上需求图所示,

/**

随机模拟数据

*/

- (void)getShowData:(CLLocationCoordinate2D)coor {

imagePointArr= [NSMutableArray array];

customPointArr= [NSMutableArray array];

NSMutableArray*dataArr = [NSMutableArray array];

for(inti =0; i <20; i++) {

doublelat =(arc4random() %100) *0.001f;

doublelon =(arc4random() %100) *0.001f;

//为了保证数据一样,所以用数组将随机产生的将纬度存起来

CLLocationCoordinate2D coorrr =CLLocationCoordinate2DMake(coor.latitude+ lat, coor.longitude+ lon);

NSDictionary *latLonDic =@{@"lat":[NSStringstringWithFormat:@"%f",coorrr.latitude],@"lon":[NSString stringWithFormat:@"%f",coorrr.longitude]};

[dataArr addObject:latLonDic];

customPointAnnotation= [[BMKPointAnnotation alloc]init];

customPointAnnotation.coordinate= coorrr;

customPointAnnotation.title= [NSString stringWithFormat:@"%d",i];

[customPointArr  addObject:customPointAnnotation];

[_mapView  addAnnotation:customPointAnnotation];

BMKAnnotationView *detailView = [_mapView viewForAnnotation:customPointAnnotation];

detailView.tag=20000+ i;

}

//用两个for循环是为了保证自定义的annotionview点在最下面,(先添加的点在地图最下方,安卓有一个属性可以设置覆盖物层级关系,但是ios目前还没有这个属性)

for(inti =0; i <20; i++) {

CLLocationCoordinate2D  coorrr =CLLocationCoordinate2DMake([dataArr[i][@"lat"]doubleValue], [dataArr[i][@"lon"]doubleValue]);

imagePointAnnotation= [[BMKPointAnnotation alloc]init];

imagePointAnnotation.coordinate= coorrr;

imagePointAnnotation.title= [NSStringstringWithFormat:@"%d",i];

[imagePointArr  addObject:imagePointAnnotation];

[_mapView  addAnnotation:imagePointAnnotation];

BMKAnnotationView* aview = [_mapView viewForAnnotation:imagePointAnnotation];

aview.tag=10000+i;

}

}

然后在- (BMKAnnotationView*)mapView:(BMKMapView*)mapView viewForAnnotation:(id)annotation方法里面来更改图片需要显示的图片样式,这里比较简单,只需要显示一张图片,所以就系统的BMKAnnotationView就可以了,然后设置图片,annotationView.image= [UIImageimageNamed:@"你需要显示的图片名字"]即可;,这里会有一个问题,如果图片比较小,你在点击的时候有可能要点很多次才能准确点击到你想要点击的点,这里解决办法是重写BMKAnnotationView的初始化方法,在里面设置其frame,自定义一个BMKAnnotationView,然后在.h中添加一个image,imageview属性用于显示你需要的图片

然后在.m中实现如下方法,也就是给self添加image。此时在viewForAnnotation设置图片的时候就需要使用你自定义的属性annimage来设置图片了annotationView.annimage= [UIImage imageNamed:@"地灾点.png"];

- (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString*)reuseIdentifier {

if(self= [super initWithAnnotation:annotationreuseIdentifier:reuseIdentifier]) {

[self setBounds:CGRectMake(0.f,0.f,50.f,50.f)];

[self setBackgroundColor:[UIColorclearColor]];

_annotationImageView= [[UIImageViewalloc]initWithFrame:self.bounds];

_annotationImageView.contentMode=UIViewContentModeCenter;

[selfaddSubview:_annotationImageView];

}

returnself;

}

- (void)setAnnimage:(UIImage*)annimage {

_annimage= annimage;

[self updateImageView];

}

- (void)updateImageView {

if([_annotationImageView isAnimating]) {

[_annotationImageView stopAnimating];

}

_annotationImageView.image=_annimage;

_annotationImageView.animationDuration=0.5;

_annotationImageView.animationRepeatCount=0;

[_annotationImageView startAnimating];

}

如何将那个自定义view也作为标注点显示呢,官方api都只是给出了一个image属性供我们设置,这里也只能自定义来处理了,依然是重写BMKAnnotationView的初始化方法,在初始化方法中在添加一个uiview参数,在初始化的时候将你需要显示的view传过去,代码如下:

- (id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString*)reuseIdentifier customView:(UIView*)detailView {

if(self= [super initWithAnnotation:annotationreuseIdentifier:reuseIdentifier]) {

[self setBounds:CGRectMake(0.f,0.f,142.f,120.f)];

self.backgroundColor= [UIColor clearColor];

self.canShowCallout=NO;

self.centerOffset=CGPointMake(40, -80);//设置中心点偏移

[self addSubview:detailView];

}

returnself;

}

viewForAnnotation代理方法中代码如下:

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

if(annotation ==imagePointAnnotation) {

NSString*AnnotationViewID =@"imageMark";

ImageAnnoTationView*annotationView = [[ImageAnnoTationView alloc]initWithAnnotation:annotationreuseIdentifier:AnnotationViewID];

//设置颜色

//annotationView.pinColor = BMKPinAnnotationColorPurple;

//从天上掉下效果

annotationView.annimage= [UIImageimageNamed:@"地灾点.png"];

//annotationView.animatesDrop = YES;

returnannotationView;

}elseif(annotation ==customPointAnnotation) {

[[NSBundlemainBundle]loadNibNamed:@"PredictView"owner:selfoptions:nil];

self.customView.frame=CGRectMake(0,0,142,120);

self.customView.tag=111222333;//设置一个tag值方便后面显示隐藏调用

CustomAnnotationView*customAnnotationView = [[CustomAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"CustomID"customView:self.customView];//将你需要自定义的view传给他自己的初始化方法

returncustomAnnotationView;

}

returnnil;

}

此时点已经在地图上描出来了,然后就是点击这个标注的响应事件,百度地图中有一个代理类似于tableview的选中某行方法,-(void)mapView:(BMKMapView*)mapView didSelectAnnotationView:(BMKAnnotationView*)view

在这个方法里将你需要显示的详情view加载出来,然后在地图zoomlevel较小的时候将其设置成当前BMKAnnotationView的paopaoview

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

if(view.tag>=10000&& view.tag<20000) {

seleView= view;

[[NSBundlemainBundle]loadNibNamed:@"PredictView"owner:selfoptions:nil];

UIView*areaPaoView=[[UIView alloc]initWithFrame:CGRectMake(0,0,242,125)];

self.customView.frame=CGRectMake(90,0,142,120);

[areaPaoView addSubview:self.customView];

BMKActionPaopaoView*paopao=[[BMKActionPaopaoView alloc]initWithCustomView:areaPaoView];

view.paopaoView.hidden=YES;

if(mapView.zoomLevel>ZOOMLEVEL) {

return;

}else{

view.paopaoView= paopao;

}

}elseif(view.tag>=20000&& view.tag<30000) {

if(mapView.zoomLevel

return;

}

[selfpopVC];

}

}

最后就是在mapview的另一个代理方法中判断zoomlevel,来决定是否要显示自定义view那个标注了

*地图渲染每一帧画面过程中,以及每次需要重绘地图时(例如添加覆盖物)都会调用此接口

*@param mapView地图View

*@param status此时地图的状态

*/

- (void)mapView:(BMKMapView*)mapView onDrawMapFrame:(BMKMapStatus*)status {

NSLog(@"%f",mapView.zoomLevel);

if(mapView.zoomLevel>ZOOMLEVEL) {

if(!isSuccessLocation) {//地图在定位过程中zoomlevel波动较大,所以在定位成功后用isSuccessLocation来标记

for(BMKPointAnnotation*annincustomPointArr) {

BMKAnnotationView* annView = [_mapViewviewForAnnotation:ann];

UIView*view = [annViewviewWithTag:111222333];

[viewsetHidden:NO];

}

//防止在地图放大之前用户已经选中某个点了

[mapViewdeselectAnnotation:seleView.annotationanimated:YES];

isSuccessLocation=YES;//避免地图放大过程中频繁调用此方法,所以在zoomlevel大于ZOOMLEVEL后就一直显示自定义view

}

}else{

if(isSuccessLocation) {

for(BMKPointAnnotation*annincustomPointArr) {

BMKAnnotationView* annView = [_mapView viewForAnnotation:ann];

UIView*view = [annView viewWithTag:111222333];

[viewsetHidden:YES];

}

isSuccessLocation=NO;

}

}

}

到这里重点部分已经讲的差不多了,第一次写文章,可能在逻辑上表达不是很清楚,有不清楚的地方看demo相信能解决你类似的需求的,附上github链接 demo下载地址,有什么不足或有什么其他实现思路的的可以一起交流讨论。

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

推荐阅读更多精彩内容