您的位置:首页 > 其它

百度地图自定义大头针图片和添加标注

2017-02-25 17:21 369 查看
// 根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView
viewForAnnotation:(id <BMKAnnotation>)annotation


    NSString * AnnotationViewID = [NSString stringWithFormat:@"renameMark%d",map_i];

    
    newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
    // 设置颜色
    ((BMKPinAnnotationView *)newAnnotation).pinColor = BMKPinAnnotationColorPurple;
    // 从天上掉下效果
    ((BMKPinAnnotationView *)newAnnotation).animatesDrop = NO;
    // 设置可拖拽
    ((BMKPinAnnotationView *)newAnnotation).draggable = YES;

    //设置大头针图标
    ((BMKPinAnnotationView *)newAnnotation).image =
[UIImage imageNamed:@"icon"];

    
    UIView * popView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 53)];

    //设置弹出气泡图片
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"yuanju_gnnr_icon"]];
    image.frame = CGRectMake(0, 0, 100, 60);
    [popView addSubview:image];

    //自定义显示的内容
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 7, 
b00d
100, 11)];
    label.text = @"医院";
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:11];
    label.textColor = [self hexStringToColor:@"#333333"];
    label.textAlignment = NSTextAlignmentLeft;
    [popView addSubview:label];

    
    UILabel * locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 25, 100, 18)];
    locationLabel.text = [self.userDefaults objectForKey:@"loc"];
    locationLabel.backgroundColor = [UIColor clearColor];
    locationLabel.font = [UIFont systemFontOfSize:18];
    locationLabel.textColor = [self hexStringToColor:@"#333333"];
    locationLabel.textAlignment = NSTextAlignmentLeft;
    [popView addSubview:locationLabel];

    //根据获取到的地址来重新设置气泡的大小
    //地址标签
    CGRect locationRect = locationLabel.frame;
    locationRect.size.width = [locationLabel sizeThatFits:CGSizeZero].width;
    locationLabel.frame = locationRect;
    //父view宽度
    CGRect popRect = popView.frame;
    popRect.size.width = [locationLabel sizeThatFits:CGSizeZero].width + 20;
    popView.frame = popRect;

    //设置弹出气泡图片
    image.frame = popView.frame;

    
    BMKActionPaopaoView * pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView];
    pView.frame = CGRectMake(0, 0, 100, 60);
    ((BMKPinAnnotationView *)newAnnotation).paopaoView = nil;
    ((BMKPinAnnotationView *)newAnnotation).paopaoView =
pView;
    map_i++;
    return newAnnotation;

    
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: