iOS高德地圖之自定義大頭針and泡泡view

啥都不說先看效果圖demo

IMG_0270.PNG

先來說說如何自定義大頭針以及點(diǎn)擊大頭針時(shí)彈出的泡泡view

一 : 自定義大頭針
  • 新建CustomAnnotationView 繼承自MAAnnotationView
  • 添加屬性
  • 重寫- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
  • 重寫- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event 解決泡泡view超出父控件事件響應(yīng)問題
  • 重寫- (void)setSelected:(BOOL)selected animated:(BOOL)animated
二 : 自定義泡泡View
  • 新建自定義氣泡類 CustomCalloutView,繼承 UIView舍哄。
  • 在 CustomCalloutView.h 中定義數(shù)據(jù)屬性侥祭,包含:圖片、商戶名和商戶地址豌注。(隨便你怎么搞,在這里我就搞了一個(gè)xib)
Snip20170620_1.png
  • 在上面新建的CustomAnnotationView.h中定義自定義氣泡屬性
#import "CustomCalloutView.h"

@interface CustomAnnotationView : MAAnnotationView

@property (nonatomic, readonly) CustomCalloutView *calloutView;

@end
  • 重寫選中方法- (void)setSelected:(BOOL)selected animated:(BOOL)animated。選中時(shí)新建并添加calloutView,傳入數(shù)據(jù)瑰妄;非選中時(shí)刪除calloutView。
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (self.selected == selected)
    {
        return;
    }
    
    if (selected)
    {
        if (self.calloutView == nil)
        {
            /* Construct custom callout. */
            
            self.calloutView = [CustomCalloutView calloutView];
            self.calloutView.frame = 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 addSubview:self.calloutView];
    }
    else
    {
        [self.calloutView removeFromSuperview];
    }
    
    [super setSelected:selected animated:animated];
}
  • 修改ViewController.m映砖,在MAMapViewDelegate的回調(diào)方法mapView:viewForAnnotation中的修改annotationView的類型

#pragma mark - MAMapViewDelegate

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *customReuseIndetifier = @"customReuseIndetifier";
        
        CustomAnnotationView *annotationView = (CustomAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:customReuseIndetifier];
        
        if (annotationView == nil)
        {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:customReuseIndetifier];
            // must set to NO, so we can show the custom callout view.
            annotationView.canShowCallout = NO;
            annotationView.draggable = YES;
            annotationView.calloutOffset = CGPointMake(0, -5);
        }
        
        return annotationView;
    }
    
    return nil;
}

  • 用于調(diào)整泡泡view顯示不全問題
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view
{
    /* Adjust the map center in order to show the callout view completely. */
    if ([view isKindOfClass:[CustomAnnotationView class]]) {
        CustomAnnotationView *cusView = (CustomAnnotationView *)view;
        CGRect frame = [cusView convertRect:cusView.calloutView.frame toView:self.mapView];
        
        frame = UIEdgeInsetsInsetRect(frame, UIEdgeInsetsMake(kCalloutViewMargin, kCalloutViewMargin, kCalloutViewMargin, kCalloutViewMargin));
        
        if (!CGRectContainsRect(self.mapView.frame, frame))
        {
            /* Calculate the offset to make the callout view show up. */
            CGSize offset = [self offsetToContainRect:frame inRect:self.mapView.frame];
            
            CGPoint theCenter = self.mapView.center;
            theCenter = CGPointMake(theCenter.x - offset.width, theCenter.y - offset.height);
            
            CLLocationCoordinate2D coordinate = [self.mapView convertPoint:theCenter toCoordinateFromView:self.mapView];
            
            [self.mapView setCenterCoordinate:coordinate animated:YES];
        }
        
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末间坐,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子邑退,更是在濱河造成了極大的恐慌竹宋,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,695評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件地技,死亡現(xiàn)場(chǎng)離奇詭異蜈七,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)莫矗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門飒硅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人作谚,你說我怎么就攤上這事三娩。” “怎么了妹懒?”我有些...
    開封第一講書人閱讀 168,130評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵雀监,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我眨唬,道長(zhǎng)会前,這世上最難降的妖魔是什么好乐? 我笑而不...
    開封第一講書人閱讀 59,648評(píng)論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮回官,結(jié)果婚禮上曹宴,老公的妹妹穿的比我還像新娘。我一直安慰自己歉提,他們只是感情好笛坦,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著苔巨,像睡著了一般版扩。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上侄泽,一...
    開封第一講書人閱讀 52,268評(píng)論 1 309
  • 那天礁芦,我揣著相機(jī)與錄音,去河邊找鬼悼尾。 笑死柿扣,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的闺魏。 我是一名探鬼主播未状,決...
    沈念sama閱讀 40,835評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼析桥!你這毒婦竟也來了司草?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤泡仗,失蹤者是張志新(化名)和其女友劉穎埋虹,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體娩怎,經(jīng)...
    沈念sama閱讀 46,286評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡搔课,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了截亦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辣辫。...
    茶點(diǎn)故事閱讀 40,505評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖魁巩,靈堂內(nèi)的尸體忽然破棺而出急灭,到底是詐尸還是另有隱情,我是刑警寧澤谷遂,帶...
    沈念sama閱讀 36,185評(píng)論 5 350
  • 正文 年R本政府宣布葬馋,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏畴嘶。R本人自食惡果不足惜蛋逾,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評(píng)論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望窗悯。 院中可真熱鬧区匣,春花似錦、人聲如沸蒋院。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽欺旧。三九已至姑丑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間辞友,已是汗流浹背栅哀。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留称龙,地道東北人留拾。 一個(gè)月前我還...
    沈念sama閱讀 48,921評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像鲫尊,于是被迫代替她去往敵國和親痴柔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容