iOS-百度地圖自定義氣泡
IOS百度地圖自定義大頭針和氣泡
重點(diǎn)是定義了一個(gè)全局變量來取氣泡的數(shù)據(jù)模型:
static int paopaoIndex = 0;
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
// 生成重用標(biāo)示identifier
NSString *AnnotationViewID = @"xidanMark";
// 檢查是否有重用的緩存
BMKAnnotationView* annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
// 緩存沒有命中靠益,自己構(gòu)造一個(gè),一般首次添加annotation代碼會(huì)運(yùn)行到此處
if (annotationView == nil) {
annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
// ((BMKPinAnnotationView*)annotationView).pinColor = BMKPinAnnotationColorRed;
[self setUpPinStyleWithImageName:@"xyRedPin" view:((BMKPinAnnotationView*)annotationView)];
// 設(shè)置重天上掉下的效果(annotation)
((BMKPinAnnotationView*)annotationView).animatesDrop = YES;
}
// 設(shè)置位置
annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5));
annotationView.annotation = annotation;
// 單擊彈出泡泡,彈出泡泡前提annotation必須實(shí)現(xiàn)title屬性
annotationView.canShowCallout = YES;
// 設(shè)置是否可以拖拽
annotationView.draggable = NO;
annotationView.paopaoView=[self customPaoPaoView:paopaoIndex];
paopaoIndex ++;
return annotationView;
}
- (BMKActionPaopaoView *)customPaoPaoView:(int)index{
XYShopListModel *shopListModel = (XYShopListModel *)_resultList[index];
//1.自定義內(nèi)容氣泡
UIView *paoView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 300, 44)];
paoView.backgroundColor=[UIColor clearColor];
//2.背景圖片
UIImageView *background = [[UIImageView alloc] initWithFrame:paoView.frame];
background.image = [UIImage imageNamed:@"paopaoBackground"];
background.layer.cornerRadius=8;
background.layer.masksToBounds=YES;
[paoView addSubview:background];
//3.導(dǎo)航按鈕
UIButton *navigationBtn = [self customNavigationBtn];
[paoView addSubview:navigationBtn];
//6.添加向右圖標(biāo)
UIImageView *rightIcon = [UIImageView new];
rightIcon.image = [UIImage imageNamed:@"dropdownmune"];
[background addSubview:rightIcon];
rightIcon.sd_layout
.rightEqualToView(background)
.topSpaceToView(background, 15)
.widthIs(rightIconwidth)
.heightIs(10);
//詳情holderView
UIView *detailHolderView = [self detailHolderViewWith:shopListModel];
[paoView addSubview:detailHolderView];
[detailHolderView setYh_x:navigationBtn_width];
[detailHolderView setYh_y:0];
//detailHolderView是動(dòng)態(tài)變的,所以要刷新一下底部的view新的寬
CGFloat allWidth = navigationBtn_width + rightIconwidth + detailHolderView.frame.size.width;
[paoView setYh_width: allWidth];
[background setYh_width:allWidth];
BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView:paoView];
return paopao;
}