iOS百度地图自定义大头针和气泡内的内容

来源:互联网 发布:面具淘宝 编辑:程序博客网 时间:2024/05/01 08:32

       最近一直在研究百度地图的自定义大头钉和气泡,这个是iOS端我的一些体会,也有自己搜集的一些资料,希望能对大家有所帮助,还有一篇Android端的,欢迎批评指正。

      先上效果图

    1、自定义大头针,根据需要自定义不同的大头针

    

代码:

       /**
 *根据anntation生成对应的View
 *@param mapView 地图View
 *@param annotation 指定的标注
 *@return 生成的标注View
 */
- (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
    // 生成重用标示identifier
    NSString *AnnotationViewID = @"xidanMark";
    
    // 检查是否有重用的缓存
    BMKAnnotationView* annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    
    // 缓存没有命中,自己构造一个,一般首次添加annotation代码会运行到此处
    if (annotationView == nil) {
        annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        
        ((BMKPinAnnotationView*)annotationView).pinColor = BMKPinAnnotationColorRed;
        // 设置重天上掉下的效果(annotation)
        ((BMKPinAnnotationView*)annotationView).animatesDrop = YES;
    }

     //根据自己的需要放图片,大小自己调整图片大小,我用的是50*50

     annotationView.image = [UIImage imageNamed:@"annomation"];

        annotationView.annotation = annotation;
    // 单击弹出泡泡,弹出泡泡前提annotation必须实现title属性
    annotationView.canShowCallout = YES;
    // 设置是否可以拖拽
    annotationView.draggable = NO;
    
    return annotationView;

}


2、自定义气泡内容




说明:上面三张图片,第一张气泡是自带的,第二张第三张是因为我没有加气泡图片,加上也很简单,但是需要注意的气泡的大小和里面label的位置


代码:

还是在刚刚那个代理方法里面

第一种效果

   UIImageView *rightImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rightImage"]];
    annotationView.rightCalloutAccessoryView =rightImage;
   
    UIImageView *leftImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"leftImage"]];
    annotationView.leftCalloutAccessoryView = leftImage;

第二种效果

          //气泡       

           UIView *areaPaoView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];
            areaPaoView.layer.cornerRadius=8;
            areaPaoView.layer.masksToBounds=YES;
            areaPaoView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];

            //第一行
            UILabel * labelfirst = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 190, 30)];
            labelfirst.text = [NSString stringWithFormat:@"本公司未铺设设备"];
            labelfirst.textColor = [UIColor blackColor];
            labelfirst.backgroundColor = [UIColor clearColor];
            [areaPaoView addSubview:labelfirst];

           //第二行
            UILabel * labelStationName = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 190, 30)];
            labelStationName.text = [NSString stringWithFormat:@"%@",annotation.title];
            labelStationName.textColor = [UIColor blackColor];
            labelStationName.backgroundColor = [UIColor clearColor];
            [areaPaoView addSubview:labelStationName];

            //这个两行代码才是整个问题的关键
            BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView:areaPaoView];
            annotationView.paopaoView=paopao;



第三种效果

   在第二种效果下,放大气泡的大小,在添加一行



给个完整的代码,可以直接复制到

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

这里面中来的

 - (BMKAnnotationView *)mapView:(BMKMapView *)view viewForAnnotation:(id <BMKAnnotation>)annotation
{
    // 生成重用标示identifier
    NSString *AnnotationViewID = @"xidanMark";
    
    // 检查是否有重用的缓存
    BMKAnnotationView* annotationView = [view dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
    
    // 缓存没有命中,自己构造一个,一般首次添加annotation代码会运行到此处
    if (annotationView == nil) {
        annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
        
        ((BMKPinAnnotationView*)annotationView).pinColor = BMKPinAnnotationColorRed;
        // 设置重天上掉下的效果(annotation)
        ((BMKPinAnnotationView*)annotationView).animatesDrop = YES;
    }

    //改成1 可实现效果2

       if(0)

         {

         //气泡       

           UIView *areaPaoView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 50)];
            areaPaoView.layer.cornerRadius=8;
            areaPaoView.layer.masksToBounds=YES;
            areaPaoView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.8];

            //第一行
            UILabel * labelfirst = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 190, 30)];
            labelfirst.text = [NSString stringWithFormat:@"本公司未铺设设备"];
            labelfirst.textColor = [UIColor blackColor];
            labelfirst.backgroundColor = [UIColor clearColor];
            [areaPaoView addSubview:labelfirst];

           //第二行
            UILabel * labelStationName = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, 190, 30)];
            labelStationName.text = [NSString stringWithFormat:@"%@",annotation.title];
            labelStationName.textColor = [UIColor blackColor];
            labelStationName.backgroundColor = [UIColor clearColor];
            [areaPaoView addSubview:labelStationName];

            //这个两行代码才是整个问题的关键
            BMKActionPaopaoView *paopao=[[BMKActionPaopaoView alloc]initWithCustomView:areaPaoView];
            annotationView.paopaoView=paopao;

         }


   UIImageView *rightImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rightImage"]];
    annotationView.rightCalloutAccessoryView =rightImage;
   
    UIImageView *leftImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"leftImage"]];
    annotationView.leftCalloutAccessoryView = leftImage;

    // 设置位置
    //    annotationView.centerOffset = CGPointMake(0, -(annotationView.frame.size.height * 0.5));
    annotationView.annotation = annotation;
    // 单击弹出泡泡,弹出泡泡前提annotation必须实现title属性
    annotationView.canShowCallout = YES;
    // 设置是否可以拖拽
    annotationView.draggable = NO;
    
    return annotationView;
}




  demo  URL:暂时没有写稍后

                        我修改下项目中的代码上传




 

0 0
原创粉丝点击