1. 程式人生 > >iOS開發之百度地圖大頭針的自定義解決方法

iOS開發之百度地圖大頭針的自定義解決方法

方法1

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

{

if ([annotation isKindOfClass:[BMKPointAnnotationclass]]) {

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

];

        newAnnotationView.pinColor=BMKPinAnnotationColorPurple;

        newAnnotationView.animatesDrop=YES;// 設定該標註點動畫顯示

        newAnnotationView.annotation=annotation;

        newAnnotationView.image = [UIImageimageNamed:@"IcoAddress.png"];   //把大頭針換成別的圖片

return newAnnotationView;

    }

returnnil;

}

方法二:如果新增文字

利用繼承

方法三:畫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;

}