欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-22 09:10:52
一、自定义大头针和气泡 // 根据anntation生成对应的view - (bmkannotationview *)mapview:(bmkmapview...

一、自定义大头针和气泡

// 根据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;
}

三、添加标注自定义气泡

1.首先实现添加多个标注和自定义气泡

添加自定义标注

[_mapview addannotations:array];

arry 中放入标注(bmkpointannotation)的数组,此方法添加多个标注。

当添加多个标注时就触发以下代理方法

#pragma mark -- bmkmapdelegate
/**
 *根据anntation生成对应的view
 *@param mapview 地图view
 *@param annotation 指定的标注
 *@return 生成的标注view
 */
-(bmkannotationview *)mapview:(bmkmapview *)mapview viewforannotation:(id <bmkannotation>)annotation
{
 if ([annotation iskindofclass:[bmkpointannotation class]]) {
  bmkpinannotationview *newannotationview = [[bmkpinannotationview alloc]initwithannotation:annotation reuseidentifier:@"myannotation"];
  newannotationview.animatesdrop = yes;
  newannotationview.annotation = annotation;
  //这里我根据自己需要,继承了bmkpointannotation,添加了标注的类型等需要的信息
  mybmkpointannotation *tt = (mybmkpointannotation *)annotation;
  //判断类别,需要添加不同类别,来赋予不同的标注图片
  if (tt.profnumber == 100000) {
   newannotationview.image = [uiimage imagenamed:@"ic_map_mode_category_merchants_normal.png"];
  }else if (tt.profnumber == 100001){
  }
  //设定popview的高度,根据是否含有缩略图
  double popviewh = 60;
  if (annotation.subtitle == nil) {
   popviewh = 38;
  }
  uiview *popview = [[uiview alloc]initwithframe:cgrectmake(0, 0, screenwidth-100, popviewh)];
  popview.backgroundcolor = [uicolor whitecolor];
  [popview.layer setmaskstobounds:yes];
  [popview.layer setcornerradius:3.0];
  popview.alpha = 0.9;
//  //设置弹出气泡图片
//  uiimageview *image = [[uiimageview alloc]initwithimage:[uiimage imagenamed:tt.imgpath]];
//  image.frame = cgrectmake(0, 160, 50, 60);
//  [popview addsubview:image];
  //自定义气泡的内容,添加子控件在popview上
  uilabel *drivername = [[uilabel alloc]initwithframe:cgrectmake(8, 4, 160, 30)];
  drivername.text = annotation.title;
  drivername.numberoflines = 0;
  drivername.backgroundcolor = [uicolor clearcolor];
  drivername.font = [uifont systemfontofsize:15];
  drivername.textcolor = [uicolor blackcolor];
  drivername.textalignment = nstextalignmentleft;
  [popview addsubview:drivername];
  uilabel *carname = [[uilabel alloc]initwithframe:cgrectmake(8, 30, 180, 30)];
  carname.text = annotation.subtitle;
  carname.backgroundcolor = [uicolor clearcolor];
  carname.font = [uifont systemfontofsize:11];
  carname.textcolor = [uicolor lightgraycolor];
  carname.textalignment = nstextalignmentleft;
  [popview addsubview:carname];
  if (annotation.subtitle != nil) {
  uibutton *searchbn = [[uibutton alloc]initwithframe:cgrectmake(170, 0, 50, 60)];
  [searchbn settitle:@"查看路线" forstate:uicontrolstatenormal];
  searchbn.backgroundcolor = maincolor;
   searchbn.titlelabel.numberoflines = 0;
  [searchbn addtarget:self action:@selector(searchline)];
  [popview addsubview:searchbn];
  }
  bmkactionpaopaoview *pview = [[bmkactionpaopaoview alloc]initwithcustomview:popview];
  pview.frame = cgrectmake(0, 0, screenwidth-100, popviewh);
  ((bmkpinannotationview*)newannotationview).paopaoview = nil;
  ((bmkpinannotationview*)newannotationview).paopaoview = pview;
  return newannotationview;
 }
 return nil;
}

点击标注和气泡响应方法

/**
 * 当选中一个annotation views时,调用此接口
 * @param mapview 地图view
 * @param views 选中的annotation views
 */
- (void)mapview:(bmkmapview *)mapview didselectannotationview:(bmkannotationview *)view
{
 _shopcoor = view.annotation.coordinate;
}
/**
 * 选中气泡调用方法
 * @param mapview 地图
 * @param view annotation
 */
- (void)mapview:(bmkmapview *)mapview annotationviewforbubble:(bmkannotationview *)view
{
 mybmkpointannotation *tt = (mybmkpointannotation *)view.annotation;
 if (tt.shopid) {
  businessifonuvc *businessifonvc = [[businessifonuvc alloc]init];
  businessifonvc.shopid = tt.shopid;
  [self.navigationcontroller pushviewcontroller:businessifonvc animated:yes];
 }
}

2.实现路线搜索,路径规划,获取街道名称等功能

通过经纬度获取地址,逆地理编码

-(void)getstartaddress
{
 //起点地址
 clgeocoder *geocoder = [[clgeocoder alloc]init];
 clgeocodecompletionhandler handler = ^(nsarray *place,nserror *error){
  for(clplacemark *placemark in place){
   nsstring *tmp = [[nsstring alloc]init];
   tmp = placemark.subthoroughfare;
   if (tmp == nil) {
    tmp = @"";
   }
   nsstring *startadr = [[nsstring alloc]initwithformat:@"%@%@",placemark.thoroughfare,tmp];
   _startcitytext.text = placemark.locality;
   if ([startadr isequaltostring:@"(null)"]) {
    _startaddrtext.text = @"获取地址失败";
   }else{
   _startaddrtext.text = startadr;
   }
 }
 };
 cllocation *loc = [[cllocation alloc]initwithlatitude:self.startcoor.latitude longitude:self.startcoor.longitude];
 [geocoder reversegeocodelocation:loc completionhandler:handler];
}

路径检索,该部分没有整理,将乘车和换乘信息放到了lineinfo,steps等模型中。

- (void)ongettransitrouteresult:(bmkroutesearch*)searcher result:(bmktransitrouteresult*)result errorcode:(bmksearcherrorcode)error
{
 nsmutablearray *linearr = [[nsmutablearray alloc]init];
 nsarray* array = [nsarray arraywitharray:_mapview.annotations];
 [_mapview removeannotations:array];
 array = [nsarray arraywitharray:_mapview.overlays];
 [_mapview removeoverlays:array];
 if (error == bmk_search_no_error) {
  for(int j = 0; j < [result.routes count];j++)
  {
  nsmutablearray *bustitlearr = [[nsmutablearray alloc]init];
  nsmutablearray *linestepsarr = [[nsmutablearray alloc]init];
  nsmutablearray *stepsarr = [[nsmutablearray alloc]init];
 //生成数据模型
  lineinfomodel *lineinfo = [[lineinfomodel alloc]init];
  bmktransitrouteline* plan = (bmktransitrouteline*)[result.routes objectatindex:j];
 //数据模型:获得路线长度
  lineinfo.distance = plan.distance;
 //数据模型:获得路线消耗时间
  lineinfo.dates = plan.duration.dates;
  lineinfo.hours = plan.duration.hours;
  lineinfo.minutes = plan.duration.minutes;
  lineinfo.seconds = plan.duration.seconds;
 // 获得轨迹点
  lineinfo.planstepsarr = plan.steps;
  // 计算路线方案中的路段数目
  int size = [plan.steps count];
  int planpointcounts = 0;
  for (int i = 0; i < size; i++) {
   bmktransitstep* transitstep = [plan.steps objectatindex:i];
 //数据模型:获得乘坐公交数组
   dlog(@"%@",transitstep.vehicleinfo.title);
   if (transitstep.vehicleinfo.title) {
    [bustitlearr addobject:transitstep.vehicleinfo.title];
   }
 //数据模型:获取换乘信息
   if (transitstep.instruction) {
    transitstep.instruction = [transitstep.instruction stringbyreplacingoccurrencesofstring:@"<font color=\"#313233\">" withstring:@""];
    [linestepsarr addobject:transitstep.instruction];
   }
   dlog(@"%@ %@",transitstep.vehicleinfo.title,transitstep.instruction);
   if(i==0){
    routeannotation* item = [[routeannotation alloc]init];
    item.coordinate = plan.starting.location;
    item.title = @"起点";
    item.type = 0;
//    [_mapview addannotation:item]; // 添加起点标注
//  
//
    [stepsarr addobject:item];
   }else if(i==size-1){
    routeannotation* item = [[routeannotation alloc]init];
    item.coordinate = plan.terminal.location;
    item.title = @"终点";
    item.type = 1;
    [stepsarr addobject:item];
//    [_mapview addannotation:item]; // 添加起点标注
//   
   }
   routeannotation* item = [[routeannotation alloc]init];
   item.coordinate = transitstep.entrace.location;
   item.title = transitstep.instruction;
   transitstep.instruction = [transitstep.instruction stringbyreplacingoccurrencesofstring:@"<font color=\"#313233\">" withstring:@""];
   item.type = 3;
   [stepsarr addobject:item];
//   [_mapview addannotation:item];
// 
//   
//   //轨迹点总数累计
   planpointcounts += transitstep.pointscount;
  }
  lineinfo.vehicleinfoarr = bustitlearr;
  lineinfo.linestepsarr = linestepsarr;
  lineinfo.stepsarr = stepsarr;
  lineinfo.planpointcounts = planpointcounts;
  [linearr addobject:lineinfo];
//  //轨迹点
//  bmkmappoint * temppoints = new bmkmappoint[planpointcounts];
//  int i = 0;
//  for (int j = 0; j < size; j++) {
//   bmktransitstep* transitstep = [plan.steps objectatindex:j];
//   int k=0;
//   for(k=0;k<transitstep.pointscount;k++) {
//    temppoints[i].x = transitstep.points[k].x;
//    temppoints[i].y = transitstep.points[k].y;
//    i++;
//   }
  }
  self.linestatusarr = linearr;
  // 通过points构建bmkpolyline
//  bmkpolyline* polyline = [bmkpolyline polylinewithpoints:temppoints count:planpointcounts];
//  [_mapview addoverlay:polyline]; // 添加路线overlay
//  delete []temppoints;
//  }
  [_tableview reloaddata];
 }
}
- (void)ongetdrivingrouteresult:(bmkroutesearch*)searcher result:(bmkdrivingrouteresult*)result errorcode:(bmksearcherrorcode)error
{
 nsarray* array = [nsarray arraywitharray:_mapview.annotations];
 [_mapview removeannotations:array];
 array = [nsarray arraywitharray:_mapview.overlays];
 [_mapview removeoverlays:array];
 if (error == bmk_search_no_error) {
  bmkdrivingrouteline* plan = (bmkdrivingrouteline*)[result.routes objectatindex:0];
  // 计算路线方案中的路段数目
  int size = [plan.steps count];
  int planpointcounts = 0;
  for (int i = 0; i < size; i++) {
   bmkdrivingstep* transitstep = [plan.steps objectatindex:i];
   if(i==0){
    routeannotation* item = [[routeannotation alloc]init];
    item.coordinate = plan.starting.location;
    item.title = @"起点";
    item.type = 0;
    [_mapview addannotation:item]; // 添加起点标注
   }else if(i==size-1){
    routeannotation* item = [[routeannotation alloc]init];
    item.coordinate = plan.terminal.location;
    item.title = @"终点";
    item.type = 1;
    [_mapview addannotation:item]; // 添加起点标注
   }
   //添加annotation节点
   routeannotation* item = [[routeannotation alloc]init];
   item.coordinate = transitstep.entrace.location;
   item.title = transitstep.entraceinstruction;
   item.degree = transitstep.direction * 30;
   item.type = 4;
   [_mapview addannotation:item];
   //轨迹点总数累计
   planpointcounts += transitstep.pointscount;
  }
  // 添加途经点
  if (plan.waypoints) {
   for (bmkplannode* tempnode in plan.waypoints) {
    routeannotation* item = [[routeannotation alloc]init];
    item = [[routeannotation alloc]init];
    item.coordinate = tempnode.pt;
    item.type = 5;
    item.title = tempnode.name;
    [_mapview addannotation:item];
   }
  }
  //轨迹点
  bmkmappoint * temppoints = new bmkmappoint[planpointcounts];
  int i = 0;
  for (int j = 0; j < size; j++) {
   bmkdrivingstep* transitstep = [plan.steps objectatindex:j];
   int k=0;
   for(k=0;k<transitstep.pointscount;k++) {
    temppoints[i].x = transitstep.points[k].x;
    temppoints[i].y = transitstep.points[k].y;
    i++;
   }
  }
  // 通过points构建bmkpolyline
  bmkpolyline* polyline = [bmkpolyline polylinewithpoints:temppoints count:planpointcounts];
  [_mapview addoverlay:polyline]; // 添加路线overlay
  delete []temppoints;
 }
}
- (void)ongetwalkingrouteresult:(bmkroutesearch*)searcher result:(bmkwalkingrouteresult*)result errorcode:(bmksearcherrorcode)error
{
 nsarray* array = [nsarray arraywitharray:_mapview.annotations];
 [_mapview removeannotations:array];
 array = [nsarray arraywitharray:_mapview.overlays];
 [_mapview removeoverlays:array];
 if (error == bmk_search_no_error) {
  bmkwalkingrouteline* plan = (bmkwalkingrouteline*)[result.routes objectatindex:0];
  int size = [plan.steps count];
  int planpointcounts = 0;
  for (int i = 0; i < size; i++) {
   bmkwalkingstep* transitstep = [plan.steps objectatindex:i];
   if(i==0){
    routeannotation* item = [[routeannotation alloc]init];
    item.coordinate = plan.starting.location;
    item.title = @"起点";
    item.type = 0;
    [_mapview addannotation:item]; // 添加起点标注
   }else if(i==size-1){
    routeannotation* item = [[routeannotation alloc]init];
    item.coordinate = plan.terminal.location;
    item.title = @"终点";
    item.type = 1;
    [_mapview addannotation:item]; // 添加起点标注
   }
   //添加annotation节点
   routeannotation* item = [[routeannotation alloc]init];
   item.coordinate = transitstep.entrace.location;
   item.title = transitstep.entraceinstruction;
   item.degree = transitstep.direction * 30;
   item.type = 4;
   [_mapview addannotation:item];
   //轨迹点总数累计
   planpointcounts += transitstep.pointscount;
  }
  //轨迹点
  bmkmappoint * temppoints = new bmkmappoint[planpointcounts];
  int i = 0;
  for (int j = 0; j < size; j++) {
   bmkwalkingstep* transitstep = [plan.steps objectatindex:j];
   int k=0;
   for(k=0;k<transitstep.pointscount;k++) {
    temppoints[i].x = transitstep.points[k].x;
    temppoints[i].y = transitstep.points[k].y;
    i++;
   }
  }
  // 通过points构建bmkpolyline
  bmkpolyline* polyline = [bmkpolyline polylinewithpoints:temppoints count:planpointcounts];
  [_mapview addoverlay:polyline]; // 添加路线overlay
  delete []temppoints;
 }
}

3.画路径

我这里实现是跳转到另一个控制器中了,下面是他一些需要的数据

//路线长度
@property (nonatomic,assign) int distance;
//路线消耗时间
@property (nonatomic,assign) int dates;
@property (nonatomic,assign) int hours;
@property (nonatomic,assign) int minutes;
@property (nonatomic,assign) int seconds;
//交通工具数组
@property (nonatomic,strong) nsarray *vehicleinfoarr;
//换乘信息
@property (nonatomic,strong) nsarray *linestepsarr;
//节点
@property (nonatomic,strong) nsarray *stepsarr;
//轨迹点个数
@property (nonatomic,assign) int planpointcounts;
//轨迹点
@property (nonatomic,strong) nsarray *planstepsarr;

接下来是画路经,关于乘车数据的展示,就是一个tableview上添加了手势,不做解释。

-(void)drawmap
{
 bmkpointannotation* item = [[bmkpointannotation alloc]init];
 item = [_lineinfo.stepsarr firstobject];
 [_mapview setcentercoordinate:item.coordinate];
 [_mapview addannotations:_lineinfo.stepsarr];
 bmkmappoint* temppoints = (bmkmappoint *)malloc(sizeof(cllocationcoordinate2d) * _lineinfo.planpointcounts);
 int i = 0;
 for (int j = 0; j < [_lineinfo.planstepsarr count]; j++) {
   bmktransitstep* transitstep = [_lineinfo.planstepsarr objectatindex:j];
   int k=0;
   for(k=0;k<transitstep.pointscount;k++) {
    temppoints[i].x = transitstep.points[k].x;
    temppoints[i].y = transitstep.points[k].y;
    i++;
  }
 }
 bmkpolyline* polyline =[bmkpolyline polylinewithpoints:temppoints count:_lineinfo.planpointcounts];
 if (nil != polyline) {
   [_mapview addoverlay:polyline]; // 添加路线overlay
 }
 free(temppoints);
}
- (bmkoverlayview*)mapview:(bmkmapview *)map viewforoverlay:(id<bmkoverlay>)overlay
{
 if ([overlay iskindofclass:[bmkpolyline class]]) {
  bmkpolylineview* polylineview = [[bmkpolylineview alloc] initwithoverlay:overlay];
  polylineview.fillcolor = [[uicolor cyancolor] colorwithalphacomponent:1];
  polylineview.strokecolor = [[uicolor bluecolor] colorwithalphacomponent:0.7];
  polylineview.linewidth = 3.0;
  return polylineview;
 }
 return nil;
}
// 判断标注类型,来处理
- (bmkannotationview*)getrouteannotationview:(bmkmapview *)mapview viewforannotation:(mybmkpointannotation*)routeannotation
{
 bmkannotationview* view = nil;
 switch (routeannotation.type) {
  case 0:
  {
   view = [mapview dequeuereusableannotationviewwithidentifier:@"start_node"];
   if (view == nil) {
    view = [[bmkannotationview alloc]initwithannotation:routeannotation reuseidentifier:@"start_node"];
    view.image = [uiimage imagewithcontentsoffile:[self getmybundlepath1:@"images/icon_nav_start.png"]];
    view.centeroffset = cgpointmake(0, -(view.frame.size.height * 0.5));
    view.canshowcallout = true;
   }
   view.annotation = routeannotation;
  }
   break;
  case 1:
  {
   view = [mapview dequeuereusableannotationviewwithidentifier:@"end_node"];
   if (view == nil) {
    view = [[bmkannotationview alloc]initwithannotation:routeannotation reuseidentifier:@"end_node"];
    view.image = [uiimage imagewithcontentsoffile:[self getmybundlepath1:@"images/icon_nav_end.png"]];
    view.centeroffset = cgpointmake(0, -(view.frame.size.height * 0.5));
    view.canshowcallout = true;
   }
   view.annotation = routeannotation;
  }
   break;
  case 2:
  {
   view = [mapview dequeuereusableannotationviewwithidentifier:@"bus_node"];
   if (view == nil) {
    view = [[bmkannotationview alloc]initwithannotation:routeannotation reuseidentifier:@"bus_node"];
    view.image = [uiimage imagewithcontentsoffile:[self getmybundlepath1:@"images/icon_nav_bus.png"]];
    view.canshowcallout = true;
   }
   view.annotation = routeannotation;
  }
   break;
  case 3:
  {
   view = [mapview dequeuereusableannotationviewwithidentifier:@"rail_node"];
   if (view == nil) {
    view = [[bmkannotationview alloc]initwithannotation:routeannotation reuseidentifier:@"rail_node"];
    view.image = [uiimage imagewithcontentsoffile:[self getmybundlepath1:@"images/icon_nav_rail.png"]];
    view.canshowcallout = true;
   }
   view.annotation = routeannotation;
  }
   break;
  case 4:
  {
   view = [mapview dequeuereusableannotationviewwithidentifier:@"route_node"];
   if (view == nil) {
    view = [[bmkannotationview alloc]initwithannotation:routeannotation reuseidentifier:@"route_node"];
    view.canshowcallout = true;
   } else {
    [view setneedsdisplay];
   }
   uiimage* image = [uiimage imagewithcontentsoffile:[self getmybundlepath1:@"images/icon_direction.png"]];
   view.image = [image imagerotatedbydegrees:routeannotation.degree];
   view.annotation = routeannotation;
  }
   break;
  case 5:
  {
   view = [mapview dequeuereusableannotationviewwithidentifier:@"waypoint_node"];
   if (view == nil) {
    view = [[bmkannotationview alloc]initwithannotation:routeannotation reuseidentifier:@"waypoint_node"];
    view.canshowcallout = true;
   } else {
    [view setneedsdisplay];
   }
   uiimage* image = [uiimage imagewithcontentsoffile:[self getmybundlepath1:@"images/icon_nav_waypoint.png"]];
   view.image = [image imagerotatedbydegrees:routeannotation.degree];
   view.annotation = routeannotation;
  }
   break;
  default:
   break;
 }
 return view;
}
- (bmkannotationview *)mapview:(bmkmapview *)view viewforannotation:(id <bmkannotation>)annotation
{
 if ([annotation iskindofclass:[bmkpointannotation class]]) {
  return [self getrouteannotationview:view viewforannotation:(mybmkpointannotation *)annotation];
 }
 return nil;
}
- (uiimage*)imagerotatedbydegrees:(cgfloat)degrees
{
 cgfloat width = cgimagegetwidth(self.cgimage);
 cgfloat height = cgimagegetheight(self.cgimage);
 cgsize rotatedsize;
 rotatedsize.width = width;
 rotatedsize.height = height;
 uigraphicsbeginimagecontext(rotatedsize);
 cgcontextref bitmap = uigraphicsgetcurrentcontext();
 cgcontexttranslatectm(bitmap, rotatedsize.width/2, rotatedsize.height/2);
 cgcontextrotatectm(bitmap, degrees * m_pi / 180);
 cgcontextrotatectm(bitmap, m_pi);
 cgcontextscalectm(bitmap, -1.0, 1.0);
 cgcontextdrawimage(bitmap, cgrectmake(-rotatedsize.width/2, -rotatedsize.height/2, rotatedsize.width, rotatedsize.height), self.cgimage);
 uiimage* newimage = uigraphicsgetimagefromcurrentimagecontext();
 uigraphicsendimagecontext();
 return newimage;
}

四、另一种方法

/////////////////////////////// 
if ([annotation iskindofclass:[siteannotation class]]) { 
   static nsstring *identifier = @"mkannotationview"; 
   bmkannotationview *pin = [mapview dequeuereusableannotationviewwithidentifier:identifier]; 
   if (pin == nil) { 
    pin = [[bmkannotationview alloc] initwithannotation:annotation reuseidentifier:identifier]; 
    //在图中我们可以看到图标的上方,有个气泡弹窗里面写着当前用户的位置所在地 
    pin.image = [uiimage imagenamed:@"default_marker.png"]; 
   } 
   pin.annotation = annotation; 
   pin.paopaoview = [[bmkactionpaopaoview alloc]initwithcustomview:[[uiview alloc] init]]; 
   return pin; 
  } 

以上所述是小编给大家介绍的ios实现百度地图自定义大头针和气泡样式,希望对大家有所帮助

上一篇:

下一篇: