自定義一個地圖彈出氣泡

本文基于高德地圖之定義的一個可點擊氣泡。效果如下圖1.0所示:


圖1.0

實現(xiàn)原理:自定義一個LJAnnomationView繼承自高德地圖的MAAnnotationView,在LJAnnomationView上放一個自定義的按鈕(本文放了一個自定義的LJButton,LJButton的自定義方法可以見我的“自定義按鈕一”這篇文章,地址www.itdecent.cn/p/da7a32a8660d),當用戶點擊自定義的氣泡時,通過代理方法把點擊事件傳遞出去處理即可。具體實現(xiàn)代碼如下。

一、首先LJAnnomationView.h文件的實現(xiàn)代碼如下:

#import <MAMapKit/MAMapKit.h>

@protocol LJAnnomationViewDelegate<NSObject>

@optional

-(void)didselectPaopaoviewWithTag:(NSInteger)tage;

@end@interface LJAnnomationView : MAAnnotationView

//構造方法

-(instancetype)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier;

@property(weak,nonatomic)iddelegate;

@end

二、其次,LJAnnomationView.m文件的代碼如下

#import "LJAnnomationView.h"

#import "LJButton.h"

#import "Tool.h"

#define TEXE_SIZE CGSizeMake(0x1.fffffep+127f, 44)

@interface LJAnnomationView (){??

? LJButton * _buton;//用按鈕代替可點擊氣泡,這是一個自定義按鈕}@end@implementation LJAnnomationView//構造方法-(instancetype)initWithAnnotation:(id)annotation reuseIdentifier:(NSString *)reuseIdentifier

{

self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];

if (self) {

self.userInteractionEnabled = YES;

_buton = [LJButton buttonWithType:UIButtonTypeCustom];

//獲取指定文字的長度

CGFloat widt = [Tool widthForText:annotation.title withFont:[UIFont systemFontOfSize:12] withSize:TEXE_SIZE];

_buton.frame = CGRectMake(0-(widt+30)*0.5, -30, widt+30, 35);

[_buton setBackgroundImage:[UIImage imageNamed:@"popup.png"] forState:UIControlStateNormal];

[_buton setImage:[UIImage imageNamed:@"item_bike.png"] forState:UIControlStateNormal];

[_buton setTitle:annotation.title forState:UIControlStateNormal];

_buton.titleRect = CGRectMake(26, 0, widt, 35);

_buton.imageRect = CGRectMake(0, 1, 25, 25);

_buton.bagImageRect = CGRectMake(0, 0, widt+30, 35);

_buton.titleLabel.font = [UIFont systemFontOfSize:12];

_buton.titleLabel.textAlignment = NSTextAlignmentLeft;

NSString * tags = annotation.subtitle;

_buton.tag = [tags integerValue];

[_buton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[_buton addTarget:self action:@selector(buttAction:) forControlEvents:UIControlEventTouchUpInside];

[self addSubview:_buton];

}

return self;

}

//點擊按鈕事件

-(void)buttAction:(UIButton*)bt

{

//把點擊事件傳遞過去

[self.delegate didselectPaopaoviewWithTag:bt.tag];

}

//重寫這個方法,如果不重寫,用戶點擊按鈕將不響應

-(UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

UIView *view = [super hitTest:point withEvent:event];

if (view == nil) {

CGPoint tempoint = [_buton convertPoint:point fromView:self];

if (CGRectContainsPoint(_buton.bounds, tempoint)) {

view = _buton;

}

}

return view;

}

@end

三、自定義的氣泡寫好后,接下來就是使用了。如何使用呢?

首先創(chuàng)建高德地圖MAMapView,把MAMapView加到控制器中顯示出來,然后創(chuàng)建一個MAPointAnnotation,然后加到地圖中去,代碼如下:

MAPointAnnotation * ann = [[MAPointAnnotation alloc] init];

ann.coordinate = _starCoord;

ann.title = @"我是測試地址";

ann.subtitle = @"123";

[self.mapview addAnnotation:ann];

四、接著實現(xiàn)MAMapView的代理方法,在這個方法自定義氣泡即可

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation

{

if ([annotation isKindOfClass:[MAPointAnnotation class]])

{

static NSString * poiIdentfier = @"LJIdtyfire";

LJAnnomationView * ljAnnotationView = (LJAnnomationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:poiIdentfier];

if (ljAnnotationView==nil) {

ljAnnotationView = [[LJAnnomationView alloc] initWithAnnotation:annotation reuseIdentifier:poiIdentfier];

ljAnnotationView.delegate = self;

}

return ljAnnotationView;

}

return nil;

}

五、別忘了,當用戶點擊氣泡的時候,還得交互,所以還得實現(xiàn)LJAnnomationView里的協(xié)議方法。

-(void)didselectPaopaoviewWithTag:(NSInteger )tage

{

NSLog(@"氣泡被點擊");

}

最后就完成了一個自定義的氣泡了,至于其他各種不同的氣泡,都可以自定義,方法跟上面所說的差不多,當然仁者見仁,智者見智,每個人都有自己的頭腦,這不是唯一的方法,這只是其中的一種勉強可行的方法。

請各位多多指教,多交流。祝生活愉快!

? ------------蔣

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容