高德地圖自定義氣泡

自定義一個(gè)需要的氣泡view:AliMapViewCustomCalloutView,繼承自UIView

新建一個(gè)繼承自MAAnnotationView的類:AliMapViewCustomAnnotationView,需要導(dǎo)入頭文件#import<MAMapKit/MAMapKit.h>

在.m文件中引用AliMapViewCustomCalloutView.h 并重寫選中方法

//重寫選中方法setSelected。選中時(shí)新建并添加calloutView,傳入數(shù)據(jù);非選中時(shí)刪除calloutView

-(void)setSelected:(BOOL)selected animated:(BOOL)animated

{

? ? if(self.selected== selected)

? ? {

? ? ? ? return;

? ? }


? ? if(selected)

? ? {

? ? ? ? if(self.calloutView==nil)

? ? ? ? {

? ? ? ? ? ? self.calloutView = [[RRParkCalloutView 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.titleLabel.text = self.annotation.title;

? ? ? ? self.calloutView.contentLabel.text = self.annotation.subtitle;

? ? ? ? [self addSubview:self.calloutView];

? ? ? ? if([self.standardintValue] == 0) {

//設(shè)置不同狀態(tài)下的選中大頭針圖片

?? ? ? ? ? ?self.image = [UIImage imageNamed:@"parkFreeClick"];

? ? ? ? }else{

? ? ? ? ? ? self.image = [UIImage imageNamed:@"parkChargeClick"];

? ? ? ? }

? ? }

? ? else

? ? {

? ? ? ? if([self.standardintValue] == 0) {

? ? ? ? ? ? self.image = [UIImage imageNamed:@"parkFree"];

? ? ? ? }else{

? ? ? ? ? ? self.image = [UIImage imageNamed:@"parkCharge"];

? ? ? ? }

? ? ? ? [self.calloutView removeFromSuperview];

? ? }

? ? [supersetSelected:selectedanimated:animated];

}

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

? ? UIView*view = [superhitTest:pointwithEvent:event];

? ? if(view ==nil) {

? ? ? ? CGPoint tempoint = [self.calloutView.naviButton convertPoint:point fromView:self];

? ? ? ? CGPoint dePoint = [self.calloutView.detailButton convertPoint:point fromView:self];

? ? ? ? if (CGRectContainsPoint(self.calloutView.naviButton.bounds, tempoint))

? ? ? ? {

? ? ? ? ? ? view =self.calloutView.naviButton;

//點(diǎn)擊導(dǎo)航按鈕的響應(yīng)

? ? ? ? ? ? [[NSNotificationCenter defaultCenter] postNotificationName:@"naviPark" object:self.annotation.title];

? ? ? ? ? ? [self.calloutView removeFromSuperview];

? ? ? ? }else if(CGRectContainsPoint(self.calloutView.detailButton.bounds, dePoint)){

? ? ? ? ? ? view =self.calloutView.detailButton;

//點(diǎn)擊查看詳情按鈕的響應(yīng)

? ? ? ? ? ? [[NSNotificationCenter defaultCenter] postNotificationName:@"detailPark" object:self.annotation.title];

? ? ? ? ? ? [self.calloutView removeFromSuperview];

? ? ? ? }else{

? ? ? ? ? ? [self.calloutView removeFromSuperview];

? ? ? ? }

? ? }

? ? returnview;

}

在controller中調(diào)用

static NSString *reuseIndetifier = @"annotationReuseIndetifier";

? ? ? ? AliMapViewCustomAnnotationView *annotationView = (AliMapViewCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];

? ? ? ?if(annotationView ==nil)

? ? ? ? {

? ? ? ? ? ? annotationView = [[AliMapViewCustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];

? ? ? ? }


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

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

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