IOS实现百度地图自定义大头针和气泡样式

2020-01-18 19:15:28丽君

一、自定义大头针和气泡


// 根据anntation生成对应的View 
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation 
{ 
 NSString *AnnotationViewID = [NSString stringWithFormat:@"renameMark%d",i]; 
 newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID]; 
 // 设置颜色 
 ((BMKPinAnnotationView*)newAnnotation).pinColor = BMKPinAnnotationColorPurple; 
 // 从天上掉下效果 
 ((BMKPinAnnotationView*)newAnnotation).animatesDrop = YES; 
 // 设置可拖拽 
 ((BMKPinAnnotationView*)newAnnotation).draggable = YES; 
 //设置大头针图标 
 ((BMKPinAnnotationView*)newAnnotation).image = [UIImage imageNamed:@"zhaohuoche"]; 
 UIView *popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 60)]; 
 //设置弹出气泡图片 
 UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"wenzi"]]; 
 image.frame = CGRectMake(0, 0, 100, 60); 
 [popView addSubview:image]; 
 //自定义显示的内容 
 UILabel *driverName = [[UILabel alloc]initWithFrame:CGRectMake(0, 3, 100, 20)]; 
 driverName.text = @"张XX师傅"; 
 driverName.backgroundColor = [UIColor clearColor]; 
 driverName.font = [UIFont systemFontOfSize:14]; 
 driverName.textColor = [UIColor whiteColor]; 
 driverName.textAlignment = NSTextAlignmentCenter; 
 [popView addSubview:driverName]; 
 UILabel *carName = [[UILabel alloc]initWithFrame:CGRectMake(0, 25, 100, 20)]; 
 carName.text = @"京A123456"; 
 carName.backgroundColor = [UIColor clearColor]; 
 carName.font = [UIFont systemFontOfSize:14]; 
 carName.textColor = [UIColor whiteColor]; 
 carName.textAlignment = NSTextAlignmentCenter; 
 [popView addSubview:carName]; 
 BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView]; 
 pView.frame = CGRectMake(0, 0, 100, 60); 
 ((BMKPinAnnotationView*)newAnnotation).paopaoView = nil; 
 ((BMKPinAnnotationView*)newAnnotation).paopaoView = pView; 
 i++; 
 return newAnnotation; 
} 

二、气泡自定义内容

ios,自定义,大头针气泡 

 最简单,最直接的方法。。。

自定义一个 UIView

核心代码如下:


//改变标注图片和自定义气泡
-(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id<BMKAnnotation>)annotation
{
 BMKAnnotationView *annotationView=[[BMKAnnotationViewalloc]initWithAnnotation:annotationreuseIdentifier:@"myAnnotation"];
 annotationView.image =[UIImageimageNamed:@"bike.gif"];
 //自定义内容气泡
 UIView *areaPaoView=[[UIViewalloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
 areaPaoView.layer.cornerRadius=8;
 areaPaoView.layer.masksToBounds=YES;
 areaPaoView.layer.contents =(id)[UIImageimageNamed:@"pao.png"].CGImage;//这张图片是做好的透明
 //areaPaoView.backgroundColor=[UIColor whiteColor];
  if ([annotation.titleisEqualToString:@"1"]) { //假设title的标题为1,那么就把添加上这个自定义气泡内容
      UILabel * labelNo = [[UILabelalloc]initWithFrame:CGRectMake(10, 0, 200, 30)];
   labelNo.text =[NSStringstringWithFormat:@"站点编号:%@"];
   labelNo.textColor = [UIColorblackColor];
   labelNo.backgroundColor = [UIColorclearColor];
   [areaPaoViewaddSubview:labelNo];
   UILabel * labelStationName = [[UILabelalloc]initWithFrame:CGRectMake(10, 20, 200, 30)];
   labelStationName.text = [NSStringstringWithFormat:@"站点名称:昆山中学"];
   labelStationName.textColor = [UIColorblackColor];
   labelStationName.backgroundColor = [UIColorclearColor];
   [areaPaoViewaddSubview:labelStationName];
   UILabel * labelSumNum = [[UILabelalloc]initWithFrame:CGRectMake(10, 40, 200, 30)];
   labelSumNum.text = [NSStringstringWithFormat:@"总桩数:30"];
   labelSumNum.textColor = [UIColorblackColor];
   labelSumNum.backgroundColor = [UIColorclearColor];
   [areaPaoViewaddSubview:labelSumNum];
   UILabel * labelBicycleNum = [[UILabelalloc]initWithFrame:CGRectMake(10, 60, 200, 30)];
   labelBicycleNum.text = [NSStringstringWithFormat:@"可借车:20"];
   labelBicycleNum.textColor = [UIColorblackColor];
   labelBicycleNum.backgroundColor = [UIColorclearColor];
   [areaPaoViewaddSubview:labelBicycleNum];
 } 
 BMKActionPaopaoView *paopao=[[BMKActionPaopaoViewalloc]initWithCustomView:areaPaoView];
 annotationView.paopaoView=paopao;
 return annotationView;
}