您的位置:首页 > 移动开发 > IOS开发

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

2017-02-09 17:42 411 查看
        这篇文章是在我做项目的过程中遇到的一些问题的解决方法,或许并不是最好的解决方法,也可能会出现一些自己不知道的问题,如果大家有更好的方法或发现了我的错误还请指正,因为技术本身就是一个不断交流不断进步的过程。
       废话不多说进入正题:
       百度地图的基本的显示定位相信大家都会使用,这个百度的文档和demo里都有,这里不在累述,突然觉得要加上一句题外话:我配置百度地图的.framework形式的开发包一直没有成功,所以我用的是静态文件的开发包,估计是百度那边的问题。

       第一步声明:

[objc] view
plain copy

BMKPinAnnotationView * newAnnotationView;  

BMKPolyline* polyline;/**<折线*/  

NSMutableArray *annoArray;/**<大头针数组*/  

NSMutableArray *piAnnoarray;/**<PinAnnotation数组*/  

 

      第二步,对数组初始化

[objc] view
plain copy

piAnnoarray = [[NSMutableArray alloc]init];  

annoArray = [[NSMutableArray alloc]init];  

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

[objc] view
plain copy

-(void)drawAnnotation  

{  

    

      

        [piAnnoarray removeAllObjects];  

        [annoArray removeAllObjects];  

         

        float tripArrayCount = [tripArray count];  

          

        // 添加折线覆盖物 声明coors 用来放置不确定个数的折点 <span style="font-family: Arial, Helvetica, sans-serif;">tripArray中存放着我的数据</span>  

  

        CLLocationCoordinate2D * coors = (CLLocationCoordinate2D *)malloc(tripArrayCount * sizeof(CLLocationCoordinate2D));  

          

        for (int i = 0; i<tripArrayCount; i++) {  

              

           BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotation alloc]init];  

            CLLocationCoordinate2D coor;  

            coor.latitude = [[[tripArray objectAtIndex:i] objectForKey:@"axisY"] floatValue];  

              

            coor.longitude = [[[tripArray objectAtIndex:i] objectForKey:@"axisX"] floatValue];  

              

            pointAnnotation.coordinate = coor;  

              

            if (i == 0) {  

      

               /// 设置当前地图的中心点  

                [_mapView setCenterCoordinate:pointAnnotation.coordinate animated:YES] ;  

               

            }  

           

            [annoArray addObject:pointAnnotation];  

            [_mapView addAnnotation:pointAnnotation];  

              

              

            // 添加折线覆盖物  

            coors[i].latitude =  coor.latitude;  

            coors[i].longitude = coor.longitude;  

              

              

        }  

          

        polyline = [BMKPolyline polylineWithCoordinates:coors count:tripArrayCount];  

        [_mapView addOverlay:polyline];  

      

    }  

      

}  

[objc] view
plain copy

// 添加标注的代理方法  

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation  

{  

      

    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {  

       newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];  

        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;  

        newAnnotationView.canShowCallout=NO;//不显示气泡 设置这个是为了在不设置title的情况下,标注也能接收到点击事件  

          

        newAnnotationView.image = [UIImage imageNamed:@"mapannotation_down"];  

          

        //此处加for循环 去找annotation对应的序号标题  

        for (int i=0; i<tripArray.count; i++) {  

              

            CGFloat lat = [[[tripArray objectAtIndex:i] objectForKey:@"axisY"] floatValue];  

            CGFloat lng =  [[[tripArray objectAtIndex:i] objectForKey:@"axisX"] floatValue];  

            //通过判断给相对应的标注添加序号标题  

            if(annotation.coordinate.latitude == lat && annotation.coordinate.longitude ==  lng )  

            {  

                if (i==0) {//第一个标注为选中状态,并设置相对应的图片  

                    [newAnnotationView setSelected:YES animated:YES];  

                    newAnnotationView.image = [UIImage imageNamed:@"mapannotation_up"];  

                      

                }  

                //给不同的标注添加1,2,3,4,5这样的序号标题  

                UILabel *la = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, newAnnotationView.frame.size.width,newAnnotationView.frame.size.height-newAnnotationView.frame.size.height*20/69)];  

                la.backgroundColor = [UIColor clearColor];  

                la.font = [UIFont systemFontOfSize:12];  

                la.textAlignment = NSTextAlignmentCenter;  

                la.text = [NSString stringWithFormat:@"%d",i+1];  

                la.tag = LABEL_TAG_NUM+i;  

                [newAnnotationView addSubview:la];  

                  

                break;  

            }  

        }  

        //把piAnnoarray添加到数组中  

        [piAnnoarray addObject:newAnnotationView];  

        return newAnnotationView;  

    }  

    return nil;  

      

}  

[objc] view
plain copy

// Override 折线  

- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{  

    if ([overlay isKindOfClass:[BMKPolyline class]]){  

        BMKPolylineView* polylineView = [[BMKPolylineView alloc] initWithOverlay:overlay];  

        polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];//颜色  

        polylineView.lineWidth = 2.0;//宽度  

          

        return polylineView;  

    }  

    return nil;  

}  

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

[objc] view
plain copy

//当选中一个annotation views时,调用此接口  

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

{  

    //坐标  

    NSLog(@"选中一个annotation views:%f,%f",view.annotation.coordinate.latitude,view.annotation.coordinate.longitude);  

    if ([view.annotation isKindOfClass:[BMKPointAnnotation class]]) {  

        //取出piAnnoarray中的每个标注  

        for (int i = 0; i<[piAnnoarray count]; i++) {  

              

            BMKPinAnnotationView *pinAnnotation = [[BMKPinAnnotationView alloc]init];  

            pinAnnotation = [piAnnoarray objectAtIndex:i];  

            //判断他的selected状态  

            if(pinAnnotation.selected )  

            {  

                  

                pinAnnotation.image = view.image = [UIImage imageNamed:@"mapannotation_up"];  

                //重新计算Frame,如果你用的图片大小一样,则不需要重新计算 设置图片一定要放到计算<span style="font-family: Arial, Helvetica, sans-serif;">Frame的前面</span>  

  

                [self SetAonnotionLaFrame:pinAnnotation labelTag:i];  

                

                /// 设置当前地图的中心点 把选中的标注作为地图中心点  

                [_mapView setCenterCoordinate:pinAnnotation.annotation.coordinate animated:YES] ;  

            }  

            else{  

                  

                pinAnnotation.image = [UIImage imageNamed:@"mapannotation_down"];  

                  

                //重新计算Frame  

                [self SetAonnotionLaFrame:pinAnnotation labelTag:i];  

                }  

              

        }  

          

  

    }  

    

}  

[objc] view
plain copy

#pragma -mark 计算大头针上label的frame   

//20和69是我计算图标大小得出的比例  

-(void)SetAonnotionLaFrame:(BMKPinAnnotationView *)AnnotationView labelTag:(float)labelTag  

{  

    //重新计算Frame  

    UILabel *label = (UILabel *)[AnnotationView viewWithTag:LABEL_TAG_NUM+labelTag];  

    label.frame = CGRectMake(0, 0, AnnotationView.frame.size.width,AnnotationView.frame.size.height-AnnotationView.frame.size.height*20/69);  

}  

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

[objc] view
plain copy

//删除标注  

-(void)removeAnnotion  

{  

    if (annoArray != nil) {  

        [_mapView removeAnnotations:annoArray];  

    }  

}  

  

//删除折线  

-(void)removeOverlay  

{  

      

    if (polyline != nil) {  

        [_mapView removeOverlay:polyline];  

    }  

}  

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

[objc] view
plain copy

-(void)removeAnnotion  

{  

    if (annoArray != nil) {  

        [_mapView removeAnnotations:_mapView.annotations];  

    }  

}  

  

  

-(void)removeOverlay  

{  

      

    if (polyline != nil) {  

        [_mapView removeOverlays:_mapView.overlays];  

    }  

}  

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

[objc] view
plain copy

//刚翻过来的一页  

BMKPinAnnotationView *pinAnnotation1 = [[BMKPinAnnotationView alloc]init];  

  

pinAnnotation1= [piAnnoarray objectAtIndex:page-detailsViewNum];  

[pinAnnotation1 setSelected:YES animated:YES];  

  

pinAnnotation1.image = [UIImage imageNamed:@"mapannotation_up"];  

//重新计算Frame  

[self SetAonnotionLaFrame:pinAnnotation1 labelTag:page-detailsViewNum];  

/// 当前地图的中心点  

[_mapView setCenterCoordinate:pinAnnotation1.annotation.coordinate animated:YES] ;  

  

//翻过去的一页  

BMKPinAnnotationView *pinAnnotation2 = [[BMKPinAnnotationView alloc]init];  

pinAnnotation2 = [piAnnoarray objectAtIndex:page-detailsViewNum+1];  

      

[pinAnnotation2 setSelected:NO animated:YES];  

pinAnnotation2.image = [UIImage imageNamed:@"mapannotation_down"];  

//重新计算Frame  

[self SetAonnotionLaFrame:pinAnnotation2 labelTag:page-detailsViewNum+1];  

         
         效果图:
                                                                                                         


         本人技术有限,只能写出项目中遇到的一些问题自己的见解,如有错误还望大家指正。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐