iOS开发之百度地图大头针的自定义解决方法

方法1

 
  

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

{

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


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

        newAnnotationView.pinColor = BMKPinAnnotationColorPurple;

        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示

        newAnnotationView.annotation=annotation;

        newAnnotationView.image = [UIImage imageNamed:@"IcoAddress.png"];   //把大头针换成别的图片

        return newAnnotationView;

    }

    return nil;

}

方法二:如果添加文字

利用继承

方法三:画image

+(UIImage*)makePopOutViewImage:(NSString*)leftText withRightText:(NSString*)rightText font:(UIFont*)font {

    NSString *describeText = leftText;

    NSString *describe2Text = rightText;

    CGFloat  additionGraphWidth = 20;

    UILabel *label = [[UILabelalloc] initWithFrame:CGRectMake(0,0, 110, 60)];

    label.font = font;

    label.text = describeText;

    CGSize  describeTextSize =  [label.textsizeWithFont:label.font];

    label.text = describe2Text;

    CGSize  describe2TextSize =  [label.textsizeWithFont:label.font];

    CGFloat x =6;

    CGFloat y =6;

    CGFloat width = 5+describeTextSize.width+5;

    CGFloat height = 5+describeTextSize.height+5;

    CGFloat rightWidth = 5+describe2TextSize.width + 5 + 6;

    

    CGFloat arrowHeight = 6;

    CGSize size = CGSizeMake(x+width + rightWidth, y+height+arrowHeight);

    

    UIGraphicsBeginImageContextWithOptions(size,NO, 2);

    

    CGContextRef context1 =UIGraphicsGetCurrentContext();

    CGContextBeginPath(context1);

    /////////////矩形填充物

    //上边框

    CGContextMoveToPoint(context1, x - arrowHeight, y - arrowHeight);//设置起点

    

    CGContextAddLineToPoint(context1, x+width+rightWidth + arrowHeight + x, y - arrowHeight);

    //右边框

    CGContextAddLineToPoint(context1, x+width+rightWidth+arrowHeight + x , y+height+arrowHeight);

    //下边框左

    CGContextAddLineToPoint(context1, x - arrowHeight, y+height+arrowHeight );

    //左边框

    CGContextAddLineToPoint(context1, x - arrowHeight, y - arrowHeight );

    

    CGContextClosePath(context1);

    CGContextSetRGBFillColor(context1,0, 0,0, 0);//填充设置为透明

    CGContextDrawPath(context1,kCGPathFill);

    

    CGContextSetLineWidth(context1,2.0);

    CGContextSetStrokeColorWithColor(context1, [UIColorblackColor].CGColor);

    

    CGContextMoveToPoint(context1,5, 0);

    CGContextAddArcToPoint(context1, 5,0, 0,5,5);

    CGContextStrokePath(context1);

    

    CGContextRef context =UIGraphicsGetCurrentContext();

    CGContextBeginPath(context);

    /////////////矩形填充物

    //上边框

    CGContextMoveToPoint(context, x + 0.5, y + 0.5);//设置起点

    CGContextAddLineToPoint(context, x+width, y +0.5);

    //右边框

    CGContextAddLineToPoint(context, x+width , y+height);

    

    //下边框右

    CGContextAddLineToPoint(context, x+width/2 + arrowHeight, y+height);

    //箭头右

    CGContextAddLineToPoint(context, x+width/2, y + height + arrowHeight -0.5);

    //箭头左

    CGContextAddLineToPoint(context, x+width/2 - arrowHeight, y+height);

    //下边框左

    CGContextAddLineToPoint(context, x + 0.5, y+height );

    //左边框

    CGContextAddLineToPoint(context, x + 0.5, y + 0.5);

    CGContextClosePath(context);

    CGContextSetRGBFillColor(context,1, 0,0, 1.000);//填充设置为红色

    CGContextDrawPath(context,kCGPathFill);


    CGContextSetLineWidth(context,2.0);

    CGContextSetStrokeColorWithColor(context, [UIColorblackColor].CGColor);

    

    CGContextMoveToPoint(context,5, 0);

    CGContextAddArcToPoint(context, 5,0, 0,5,5);

    CGContextStrokePath(context);

    

    CGContextRef rightContext =UIGraphicsGetCurrentContext();

    CGContextBeginPath(rightContext);

    CGContextMoveToPoint(rightContext, width + x, y +0.5);

    CGContextAddLineToPoint(rightContext, width + rightWidth +0 - 0.5, y +0.5);

    CGContextAddLineToPoint(rightContext, width + rightWidth +0 - 0.5, height + y );

    CGContextAddLineToPoint(rightContext, width + x, height + y);

    CGContextAddLineToPoint(rightContext, width + x, y +0.5);

    CGContextClosePath(rightContext);

    CGContextSetRGBFillColor(rightContext, 0, 0, 0,1);

    CGContextDrawPath(rightContext,kCGPathFill);

    //绘制文字

    CGContextSetRGBFillColor(context,1.0, 1.0, 1.0,0.85); //设置为白色

    [describeText drawAtPoint:CGPointMake(11,11) withFont:font];

    

    CGContextSetRGBFillColor(rightContext, 1, 1, 1,0.85); //设置为白色

    [describe2Text drawAtPoint:CGPointMake(width +11, 11) withFont:font];

    

    UIImage* image =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;

}

你可能感兴趣的:(iOS开发)