IOS 百度地图开发 大头针标数字 大头针(标注)以及折线的基本使用

        这篇文章是在我做项目的过程中遇到的一些问题的解决方法,或许并不是最好的解决方法,也可能会出现一些自己不知道的问题,如果大家有更好的方法或发现了我的错误还请指正,因为技术本身就是一个不断交流不断进步的过程。

       废话不多说进入正题:

       百度地图的基本的显示定位相信大家都会使用,这个百度的文档和demo里都有,这里不在累述,突然觉得要加上一句题外话:我配置百度地图的.framework形式的开发包一直没有成功,所以我用的是静态文件的开发包,估计是百度那边的问题。



       第一步声明:

[objc]  view plain  copy
  1. BMKPinAnnotationView * newAnnotationView;  
  2. BMKPolyline* polyline;/**<折线*/  
  3. NSMutableArray *annoArray;/**<大头针数组*/  
  4. NSMutableArray *piAnnoarray;/**<PinAnnotation数组*/  
 



      第二步,对数组初始化

[objc]  view plain  copy
  1. piAnnoarray = [[NSMutableArray alloc]init];  
  2. annoArray = [[NSMutableArray alloc]init];  



       第三步:添加大头针(标注和)折线

[objc]  view plain  copy
  1. -(void)drawAnnotation  
  2. {  
  3.     
  4.       
  5.         [piAnnoarray removeAllObjects];  
  6.         [annoArray removeAllObjects];  
  7.          
  8.         float tripArrayCount = [tripArray count];  
  9.           
  10.         // 添加折线覆盖物 声明coors 用来放置不确定个数的折点 <span style="font-family: Arial, Helvetica, sans-serif;">tripArray中存放着我的数据</span>  
  11.   
  12.         CLLocationCoordinate2D * coors = (CLLocationCoordinate2D *)malloc(tripArrayCount * sizeof(CLLocationCoordinate2D));  
  13.           
  14.         for (int i = 0; i<tripArrayCount; i++) {  
  15.               
  16.            BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotation alloc]init];  
  17.             CLLocationCoordinate2D coor;  
  18.             coor.latitude = [[[tripArray objectAtIndex:i] objectForKey:@"axisY"] floatValue];  
  19.               
  20.             coor.longitude = [[[tripArray objectAtIndex:i] objectForKey:@"axisX"] floatValue];  
  21.               
  22.             pointAnnotation.coordinate = coor;  
  23.               
  24.             if (i == 0) {  
  25.       
  26.                /// 设置当前地图的中心点  
  27.                 [_mapView setCenterCoordinate:pointAnnotation.coordinate animated:YES] ;  
  28.                
  29.             }  
  30.            
  31.             [annoArray addObject:pointAnnotation];  
  32.             [_mapView addAnnotation:pointAnnotation];  
  33.               
  34.               
  35.             // 添加折线覆盖物  
  36.             coors[i].latitude =  coor.latitude;  
  37.             coors[i].longitude = coor.longitude;  
  38.               
  39.               
  40.         }  
  41.           
  42.         polyline = [BMKPolyline polylineWithCoordinates:coors count:tripArrayCount];  
  43.         [_mapView addOverlay:polyline];  
  44.       
  45.     }  
  46.       
  47. }  


[objc]  view plain  copy
  1. // 添加标注的代理方法  
  2. - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation  
  3. {  
  4.       
  5.     if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {  
  6.        newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];  
  7.         newAnnotationView.pinColor = BMKPinAnnotationColorPurple;  
  8.         newAnnotationView.canShowCallout=NO;//不显示气泡 设置这个是为了在不设置title的情况下,标注也能接收到点击事件  
  9.           
  10.         newAnnotationView.image = [UIImage imageNamed:@"mapannotation_down"];  
  11.           
  12.         //此处加for循环 去找annotation对应的序号标题  
  13.         for (int i=0; i<tripArray.count; i++) {  
  14.               
  15.             CGFloat lat = [[[tripArray objectAtIndex:i] objectForKey:@"axisY"] floatValue];  
  16.             CGFloat lng =  [[[tripArray objectAtIndex:i] objectForKey:@"axisX"] floatValue];  
  17.             //通过判断给相对应的标注添加序号标题  
  18.             if(annotation.coordinate.latitude == lat && annotation.coordinate.longitude ==  lng )  
  19.             {  
  20.                 if (i==0) {//第一个标注为选中状态,并设置相对应的图片  
  21.                     [newAnnotationView setSelected:YES animated:YES];  
  22.                     newAnnotationView.image = [UIImage imageNamed:@"mapannotation_up"];  
  23.                       
  24.                 }  
  25.                 //给不同的标注添加1,2,3,4,5这样的序号标题  
  26.                 UILabel *la = [[UILabel alloc]initWithFrame:CGRectMake(00, newAnnotationView.frame.size.width,newAnnotationView.frame.size.height-newAnnotationView.frame.size.height*20/69)];  
  27.                 la.backgroundColor = [UIColor clearColor];  
  28.                 la.font = [UIFont systemFontOfSize:12];  
  29.                 la.textAlignment = NSTextAlignmentCenter;  
  30.                 la.text = [NSString stringWithFormat:@"%d",i+1];  
  31.                 la.tag = LABEL_TAG_NUM+i;  
  32.                 [newAnnotationView addSubview:la];  
  33.                   
  34.                 break;  
  35.             }  
  36.         }  
  37.         //把piAnnoarray添加到数组中  
  38.         [piAnnoarray addObject:newAnnotationView];  
  39.         return newAnnotationView;  
  40.     }  
  41.     return nil;  
  42.       
  43. }  

[objc]  view plain  copy
  1. // Override 折线  
  2. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{  
  3.     if ([overlay isKindOfClass:[BMKPolyline class]]){  
  4.         BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];  
  5.         polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];//颜色  
  6.         polylineView.lineWidth = 2.0;//宽度  
  7.           
  8.         return polylineView;  
  9.     }  
  10.     return nil;  
  11. }  



       第四步,大头针(标注)的点击事件

[objc]  view plain  copy
  1. //当选中一个annotation views时,调用此接口  
  2. - (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view  
  3. {  
  4.     //坐标  
  5.     NSLog(@"选中一个annotation views:%f,%f",view.annotation.coordinate.latitude,view.annotation.coordinate.longitude);  
  6.     if ([view.annotation isKindOfClass:[BMKPointAnnotation class]]) {  
  7.         //取出piAnnoarray中的每个标注  
  8.         for (int i = 0; i<[piAnnoarray count]; i++) {  
  9.               
  10.             BMKPinAnnotationView *pinAnnotation = [[BMKPinAnnotationView alloc]init];  
  11.             pinAnnotation = [piAnnoarray objectAtIndex:i];  
  12.             //判断他的selected状态  
  13.             if(pinAnnotation.selected )  
  14.             {  
  15.                   
  16.                 pinAnnotation.image = view.image = [UIImage imageNamed:@"mapannotation_up"];  
  17.                 //重新计算Frame,如果你用的图片大小一样,则不需要重新计算 设置图片一定要放到计算<span style="font-family: Arial, Helvetica, sans-serif;">Frame的前面</span>  
  18.   
  19.                 [self SetAonnotionLaFrame:pinAnnotation labelTag:i];  
  20.                 
  21.                 /// 设置当前地图的中心点 把选中的标注作为地图中心点  
  22.                 [_mapView setCenterCoordinate:pinAnnotation.annotation.coordinate animated:YES] ;  
  23.             }  
  24.             else{  
  25.                   
  26.                 pinAnnotation.image = [UIImage imageNamed:@"mapannotation_down"];  
  27.                   
  28.                 //重新计算Frame  
  29.                 [self SetAonnotionLaFrame:pinAnnotation labelTag:i];  
  30.                 }  
  31.               
  32.         }  
  33.           
  34.   
  35.     }  
  36.     
  37. }  

[objc]  view plain  copy
  1. #pragma -mark 计算大头针上label的frame   
  2. //20和69是我计算图标大小得出的比例  
  3. -(void)SetAonnotionLaFrame:(BMKPinAnnotationView *)AnnotationView labelTag:(float)labelTag  
  4. {  
  5.     //重新计算Frame  
  6.     UILabel *label = (UILabel *)[AnnotationView viewWithTag:LABEL_TAG_NUM+labelTag];  
  7.     label.frame = CGRectMake(00, AnnotationView.frame.size.width,AnnotationView.frame.size.height-AnnotationView.frame.size.height*20/69);  
  8. }  



       第五步,删除标注(大头针)和折线 

[objc]  view plain  copy
  1. //删除标注  
  2. -(void)removeAnnotion  
  3. {  
  4.     if (annoArray != nil) {  
  5.         [_mapView removeAnnotations:annoArray];  
  6.     }  
  7. }  
  8.   
  9. //删除折线  
  10. -(void)removeOverlay  
  11. {  
  12.       
  13.     if (polyline != nil) {  
  14.         [_mapView removeOverlay:polyline];  
  15.     }  
  16. }  

以上方法也可以删除所有的标注和折线,但有的时候可能会出现无法完全删除的情况,所以推荐使用下面的方法去删除

 

[objc]  view plain  copy
  1. -(void)removeAnnotion  
  2. {  
  3.     if (annoArray != nil) {  
  4.         [_mapView removeAnnotations:_mapView.annotations];  
  5.     }  
  6. }  
  7.   
  8.   
  9. -(void)removeOverlay  
  10. {  
  11.       
  12.     if (polyline != nil) {  
  13.         [_mapView removeOverlays:_mapView.overlays];  
  14.     }  
  15. }  


       题外话:因为我做的是有翻页的效果的,就是UIScrollView每翻一页,标注的的选中状态就会发生变化,原来选中的标注变为非选中,在他之前或之后的现在为选中状态,下面是这个效果代码的一个效果,其他的大家以此类推:

[objc]  view plain  copy
  1. //刚翻过来的一页  
  2. BMKPinAnnotationView *pinAnnotation1 = [[BMKPinAnnotationView alloc]init];  
  3.   
  4. pinAnnotation1= [piAnnoarray objectAtIndex:page-detailsViewNum];  
  5. [pinAnnotation1 setSelected:YES animated:YES];  
  6.   
  7. pinAnnotation1.image = [UIImage imageNamed:@"mapannotation_up"];  
  8. //重新计算Frame  
  9. [self SetAonnotionLaFrame:pinAnnotation1 labelTag:page-detailsViewNum];  
  10. /// 当前地图的中心点  
  11. [_mapView setCenterCoordinate:pinAnnotation1.annotation.coordinate animated:YES] ;  
  12.   
  13. //翻过去的一页  
  14. BMKPinAnnotationView *pinAnnotation2 = [[BMKPinAnnotationView alloc]init];  
  15. pinAnnotation2 = [piAnnoarray objectAtIndex:page-detailsViewNum+1];  
  16.       
  17. [pinAnnotation2 setSelected:NO animated:YES];  
  18. pinAnnotation2.image = [UIImage imageNamed:@"mapannotation_down"];  
  19. //重新计算Frame  
  20. [self SetAonnotionLaFrame:pinAnnotation2 labelTag:page-detailsViewNum+1];  

         

         效果图:

                                                                                                         


         本人技术有限,只能写出项目中遇到的一些问题自己的见解,如有错误还望大家指正。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值