直入正題吧!git
這些都是知道的了,看文檔添加就好了!佈局
實現三個代理方法:spa
這個方法相似tableview添加cell,都是建立annotation.net
#pragma mark #pragma mark - BMKMapview delegate -(BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation;代理
這個方法在點擊地圖marker時所觸發(並顯示callout)繼承
-(void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view;事件
這個方法在點擊地圖任意位置,至關於隱藏callout圖片
-(void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view;ci
原理:地圖上的marker是在viewForAnnoation裏建立的,同時也會 隱含的爲咱們建立一個CalloutView,就是自帶的吹出框,只是咱們看不到源碼。其實這個吹出框(CalloutView)也是一個 annotation,也會在viewForAnnotation裏被建立,他的座標應該和這個點的marker座標同樣,只要明白了這一點,就好了,marker和吹出框是兩個不一樣的annotation,他們有一樣的coordinate。rem
第一步:
自定義一個Annotation,爲了簡單方便,我就直接繼承了mapview自帶的BMKPointAnnotation,這是一個經典的圖釘marker。
如下部分是爲自定義annotation添加顯示更多信息
第二步:
建立一個(自定義的CalloutView)的Annotation,至關於顯示calloutView的annotation。
[注意] 繼承自NSObject<BMKAnnotation>
第三步:
這一步咱們建立自定義的View,想要什麼佈局就寫什麼樣的佈局,想要多少屬性就加多少屬性。
[注意:繼承自BMKAnnotationView]
AnnotationPointCell是ContentView裏的subview,這個view就是顯示各個組件,並賦不一樣的值
CallOutAnnotationView.m
#import "CallOutAnnotationView.h"
#define Arror_height 6
@implementation CallOutAnnotationView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(id)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.canShowCallout = NO;
self.centerOffset = CGPointMake(0, -55);
self.frame = CGRectMake(0, 0, 240, 80);
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, self.frame.size.width-15, self.frame.size.height-15)];
contentView.backgroundColor = [UIColor clearColor];
[self addSubview:contentView];
self.contentView = contentView;
} return self;
}
-(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:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0].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),
// midy = CGRectGetMidY(rrect),
maxy = CGRectGetMaxY(rrect)-Arror_height;
CGContextMoveToPoint(context, midx+Arror_height, maxy);
CGContextAddLineToPoint(context,midx, maxy+Arror_height);
CGContextAddLineToPoint(context,midx-Arror_height, 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);
}
AnnotationPointCell裏面想添加什麼內容就本身定吧
第四步:
下面是VC裏面的主要代碼
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id)annotation
{
static NSString * annotationIndentifier = @"customAnnotation";
if ([annotation isKindOfClass:[CusTomPointAnnotation class]]) {
BMKPinAnnotationView * annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationIndentifier];
/**
設置大頭針圖片
annotationView.image = [UIImage imageNamed:@""];
**/
[annotationView setAnimatesDrop:YES];
//[注意]在添加marker的判斷裏必定要設置markerannotation.canShowCallout =NO; 不然點擊marker會默認顯示系統的吹出框
annotationView.canShowCallout = NO;
return annotationView;
}else if ([annotation isKindOfClass:[CalloutMapAnnotation class]]){
//此時annotation就是咱們calloutview的annotation
CalloutMapAnnotation *ann = (CalloutMapAnnotation*)annotation;
//若是能夠重用
CallOutAnnotationView *calloutannotationview = (CallOutAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"calloutview"];
//不然建立新的calloutView
if (!calloutannotationview) {
calloutannotationview = [[CallOutAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"calloutview"];
AnnotationPointCell *cell = [[AnnotationPointCell alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
[calloutannotationview.contentView addSubview:cell];
calloutannotationview.busInfoView = cell;
}
//開始設置添加marker時的賦值
calloutannotationview.busInfoView.nameLabel.text = [ann.locationInfo objectForKey:@"alias"];
calloutannotationview.busInfoView.contentLabel.text = [ann.locationInfo objectForKey:@"speed"];
calloutannotationview.busInfoView.timeLabel.text =[ann.locationInfo objectForKey:@"degree"];
// calloutannotationview.busInfoView.titleImageView.image = [UIImage imageNamed:@""];
return calloutannotationview;
}
return nil;
}
- (void)mapView:(BMKMapView *)mapView didSelectAnnotationView:(BMKAnnotationView *)view
{
//CustomPointAnnotation 是自定義的marker標註點,經過這個來獲得添加marker時設置的pointCalloutInfo屬性
CusTomPointAnnotation *annn = (CusTomPointAnnotation*)view.annotation;
if ([view.annotation isKindOfClass:[CusTomPointAnnotation class]]) {
//若是點到了這個marker點,什麼也不作
if (_calloutMapAnnotation.coordinate.latitude == view.annotation.coordinate.latitude && _calloutMapAnnotation.coordinate.longitude == view.annotation.coordinate.longitude) {
return;
} //若是當前顯示着calloutview,又觸發了select方法,刪除這個calloutview annotation(處理多個氣泡的狀況)
if (_calloutMapAnnotation) {
[mapView removeAnnotation:_calloutMapAnnotation];
_calloutMapAnnotation=nil;
} //建立搭載自定義calloutview的annotation
_calloutMapAnnotation = [[CalloutMapAnnotation alloc] initWithLatitude:view.annotation.coordinate.latitude andLongitude:view.annotation.coordinate.longitude];
//把經過marker(ZNBCPointAnnotation)設置的pointCalloutInfo信息賦值給CalloutMapAnnotation
_calloutMapAnnotation.locationInfo = annn.pointCalloutInfo;
[mapView addAnnotation:_calloutMapAnnotation];
[mapView setCenterCoordinate:view.annotation.coordinate animated:YES];
}else if ([view isKindOfClass:[CallOutAnnotationView class]]) {
//這裏處理點擊自定義吹出框的事件
}
}
#pragma mark - 這個方法在點擊地圖任意位置,至關於隱藏callout
//這個方法在點擊地圖任意位置,至關於隱藏callout
-(void)mapView:(BMKMapView *)mapView didDeselectAnnotationView:(BMKAnnotationView *)view{
if (_calloutMapAnnotation&&![view isKindOfClass:[CallOutAnnotationView class]]&&![view isKindOfClass:[CalloutMapAnnotation class]]) {
if (_calloutMapAnnotation.coordinate.latitude == view.annotation.coordinate.latitude) {
[mapView removeAnnotation:_calloutMapAnnotation];
_calloutMapAnnotation = nil;
}
}
}