- (MAAnnotationView*)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
static NSString *annotationViewID = @"annotationViewID";
CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewID];
if (annotationView == nil) {
annotationView = [[CustomAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationViewID];
}
annotationView.canShowCallout = NO;//弹出气泡
if (isNormal) {
annotationView.image = [UIImage imageNamed:@"red"];
}
else
{
annotationView.image = [UIImage imageNamed:@"green"];
}
annotationView.titleStr = annotation.title;
annotationView.subTitleStr = annotation.subtitle;
return annotationView;
}
return nil;
}
//浮窗
#import <UIKit/UIKit.h>
@interface OfferCalloutView : UIView
@property (nonatomic, strong) UIImage *image;
@property (nonatomic,copy) NSString *subTitleStr;//副标题
@property (nonatomic,copy) NSString *titleStr;//标题
@end
#import "OfferCalloutView.h"
#import <QuartzCore/QuartzCore.h>
#define kArrorHeight 10
#define kPortraitMargin 5
#define kPortraitWidth 70
#define kPortraitHeight 50
#define kTitleWidth 120
#define kTitleHeight 40
@interface OfferCalloutView ()
@property (nonatomic, strong) UIImageView *portraitView;
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@end
@implementation OfferCalloutView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
/**
* 改这个地方的颜色就好了
它的界面是两层的,为了绘制那个小的三角,所以底层的颜色设置成透明的就好了
*/
// self.backgroundColor = [UIColor whiteColor];
self.backgroundColor = [UIColor clearColor];
[self initSubViews];
}
return self;
}
- (void)initSubViews
{
// 添加图片
self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(kPortraitMargin, kPortraitMargin, kPortraitWidth, kPortraitHeight)];
self.portraitView.backgroundColor = [UIColor blackColor];
// self.portraitView.backgroundColor = [UIColor whiteColor];
[self addSubview:self.portraitView];
// 添加标题
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin, kTitleWidth, kTitleHeight-20)];
self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
self.titleLabel.textColor = [UIColor redColor];
[self addSubview:self.titleLabel];
//添加副标题
self.subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kPortraitMargin * 2 + kPortraitWidth, kPortraitMargin * 2 + kTitleHeight-30, kTitleWidth, kTitleHeight)];
self.subtitleLabel.font = [UIFont systemFontOfSize:12];
self.subtitleLabel.numberOfLines = 0;
self.subtitleLabel.textColor = [UIColor greenColor];
[self addSubview:self.subtitleLabel];
}
#pragma mark - Override
- (void)setTitleStr:(NSString *)titleStr
{
self.titleLabel.text = titleStr;
}
- (void)setSubTitleStr:(NSString *)subTitleStr
{
self.subtitleLabel.text = subTitleStr;
}
- (void)setImage:(UIImage *)image
{
self.portraitView.image = image;
}
#pragma mark - draw rect
- (void)drawRect:(CGRect)rect
{
[self drawInContext:UIGraphicsGetCurrentContext()];
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowOpacity = 1.0;
self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
}
- (void)drawInContext:(CGContextRef)context
{
CGContextSetLineWidth(context, 2.0);
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:1 green:1 blue:1 alpha:0.8].CGColor);
[self getDrawPath:context];
CGContextFillPath(context);
}
- (void)getDrawPath:(CGContextRef)context
{
CGRect rrect = self.bounds;
CGFloat radius = 6.0;
CGFloat minx = CGRectGetMinX(rrect),
midx = CGRectGetMidX(rrect),
maxx = CGRectGetMaxX(rrect);
CGFloat miny = CGRectGetMinY(rrect),
maxy = CGRectGetMaxY(rrect)-kArrorHeight;
CGContextMoveToPoint(context, midx+kArrorHeight, maxy);
CGContextAddLineToPoint(context,midx, maxy+kArrorHeight);
CGContextAddLineToPoint(context,midx-kArrorHeight, maxy);
CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
CGContextClosePath(context);
}
@end
#import <MAMapKit/MAMapKit.h>
@class CallOutAnnotationVifew;
@class OfferCalloutView;
@interface CustomAnnotationView : MAAnnotationView
@property (nonatomic,readonly) OfferCalloutView *calloutView;
@property (nonatomic,copy) NSString *titleStr;//标题
@property (nonatomic,copy) NSString *subTitleStr;//副标题
@end
#import "CustomAnnotationView.h"
#import "OfferCalloutView.h"
@interface CustomAnnotationView ()
@property (nonatomic,strong, readwrite) OfferCalloutView *calloutView;
@end
@implementation CustomAnnotationView
@synthesize calloutView = _calloutView;
#pragma mark - Override
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
if (self.selected == selected) {
return;
}
if (selected) {
if (self.calloutView == nil) {
#define kCalloutWidth 200.0
#define kCalloutHeight 70.0
self.calloutView = [[OfferCalloutView alloc]initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight)];
self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds)/2.f + self.calloutOffset.x , -CGRectGetHeight(self.calloutView.bounds)/2.f + self.calloutOffset.y);
}
self.calloutView.image = [UIImage imageNamed:@"buting.jpg"];
self.calloutView.titleStr = _titleStr;
self.calloutView.subTitleStr = _subTitleStr;
[self addSubview:self.calloutView];
}
else
{
[self.calloutView removeFromSuperview];
self.calloutView = nil;
}
[super setSelected:selected animated:animated];
}
@end