2020-01-14 IOS原生地圖開發(fā)(4)——自定義標(biāo)記Annotation氣泡

上一篇描述了如何自定義標(biāo)記并使用代箭,以及解析csv文件讀取坐標(biāo)和屬性,本篇將自定義氣泡(簡(jiǎn)單的tableview)進(jìn)行屬性的顯示春塌。MapKit自帶的有大頭針Annotation以及基礎(chǔ)的氣泡樣式晓避,但是涉及到具體的案例中不如自己寫樣式來(lái)的方便簇捍。
本篇內(nèi)容:利用Xib自定義Annotation彈出氣泡

具體效果:


點(diǎn)擊Ann彈出氣泡顯示該點(diǎn)屬性

一、創(chuàng)建Xib

創(chuàng)建一個(gè)新的ViewController并記得勾選create Xib按鈕俏拱。完成后將在文件目錄下發(fā)現(xiàn).h .m .xib的文件組暑塑。利用該xib進(jìn)行氣泡的構(gòu)建。

新建Xib組合

二锅必、定義Xib樣式

1事格、StoryBoard 使用

利用Xib開發(fā)的好處在于頁(yè)面布局輕快簡(jiǎn)潔,熟悉StoryBorad使用后可以很輕快進(jìn)行Xib開發(fā)(注意將Xib的size屬性設(shè)置為Freedom搞隐,方便修改Xib的大芯杂蕖),如下圖:


Xib頁(yè)面布局劣纲,一個(gè)簡(jiǎn)單的tableview

2么鹤、控制器書寫

頭文件:

//
//  CallOutViewViewController.h
//  BigSpatialData
//
//  Created by Apple on 2019/10/29.
//  Copyright ? 2019 zzcBjergsen. All rights reserved.
//

#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface CallOutViewViewController : UIViewController

//this method is loaded when the proper table needed update

-(void) updatetheProperTbale:(NSMutableArray *) properArray;

@end

NS_ASSUME_NONNULL_END

實(shí)現(xiàn)文件:

//
//  CallOutViewViewController.m
//  BigSpatialData
//
//  Created by Apple on 2019/10/29.
//  Copyright ? 2019 zzcBjergsen. All rights reserved.
//

#import "CallOutViewViewController.h"

@interface CallOutViewViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *properTable;
@property (nonatomic,strong) NSMutableArray * properArray;
@end

@implementation CallOutViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //regist the delegate of table
    _properTable.delegate = self;
    _properTable.dataSource = self;
    
    //init the self.properArray
    _properArray = [[NSMutableArray alloc] init];
    
    
}


//implent the method
-(void) updatetheProperTbale:(NSMutableArray *) properArray{  
    if (properArray) {
        _properArray = properArray;
        [_properTable reloadData];
    }    
}

#pragma mark TableViewDelegate

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (_properArray) {
        
        return  _properArray.count;
    }
    
    return 1;
    
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    
    //get the string from array and regist the cell text
    NSString * cellStr = [_properArray objectAtIndex:indexPath.row];
    if (cellStr) {
        [cell.textLabel setText:cellStr];
        cell.textLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
    }else{
        [cell.textLabel setText:@"屬性:無(wú)"];
    }
    cell.backgroundColor = UIColor.greenColor;
    return cell;
    
}


@end

三、外部調(diào)用
1味廊、創(chuàng)建實(shí)例并設(shè)置好大小蒸甜、位置、透明度

@property (nonatomic, strong) CallOutViewViewController *callOutVC;
    //set the callVC init
    
    _callOutVC = [[CallOutViewViewController alloc] initWithNibName:@"CallOutViewViewController" bundle:[NSBundle mainBundle]];
    [_callOutVC.view setFrame:CGRectMake(0, 0, _callOutVC.view.frame.size.width, _callOutVC.view.frame.size.height)];
    [_callOutVC.view setCenter:self.view.center];
    _callOutVC.view.alpha = 0.0;//先隱藏
    [_mapView addSubview:_callOutVC.view];

2余佛、實(shí)現(xiàn)Annotation的點(diǎn)擊事件委托(點(diǎn)擊即顯示屬性)

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
    
    if ([view.reuseIdentifier  isEqual: @"Point_Annotation"]){
        PointAnnotation * Ann = view.annotation;
            //refresh the properTable in calloutVC
            [_callOutVC updatetheProperTbale:[self keyValueFromHuAnn:Ann]];
            
            //callout  the  calloutvc
            _callOutVC.view.alpha = 1.0;
            //record the ann's coord 記錄Annotation的屏幕坐標(biāo) 把自定義氣泡放過(guò)來(lái)
            _annCoor = Ann.coordinate;
            [_callOutVC.view setCenter:[_mapView convertCoordinate:Ann.coordinate toPointToView:_mapView]];
    }
    
}
//key - value from Hurricane Ann
-(NSMutableArray *) keyValueFromHuAnn:(PointAnnotation *) Ann{
    
    NSMutableArray * keyValueArray = [[NSMutableArray alloc] init];
    
    if (Ann.hurricane) {
        NSString * date = [NSString stringWithFormat:@"Date:%@",Ann.hurricane.date];
        NSString * time = [NSString stringWithFormat:@"Time:%@",Ann.hurricane.time];
        NSString * wind = [NSString stringWithFormat:@"Wind:%@",Ann.hurricane.wind];
        NSString * presure = [NSString stringWithFormat:@"Presure:%@",Ann.hurricane.presure];
        NSString * stormType = [NSString stringWithFormat:@"StormType:%@",Ann.hurricane.stormType];
        NSString * category = [NSString stringWithFormat:@"Category:%@",Ann.hurricane.category];
        NSString * name = [NSString stringWithFormat:@"Name:%@",Ann.hurricane.name];
        
        [keyValueArray addObject:date];
        [keyValueArray addObject:time];
        [keyValueArray addObject:wind];
        [keyValueArray addObject:presure];
        [keyValueArray addObject:stormType];
        [keyValueArray addObject:category];
        [keyValueArray addObject:name];
    }
    
    return keyValueArray;
}

3柠新、實(shí)現(xiàn)地圖視圖拖拽時(shí)的事件——開始拖動(dòng)時(shí)隱藏 結(jié)束時(shí)再出來(lái)

//當(dāng)拖拽,放大辉巡,縮小恨憎,雙擊手勢(shì)開始時(shí)調(diào)用
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    //hide the VC if it is shown
    if (_callOutVC.view.alpha == 1.0) {
        _callOutVC.view.alpha =0.0;
    }
    
}

//當(dāng)拖拽,放大郊楣,縮小憔恳,雙擊手勢(shì)結(jié)束時(shí)調(diào)用
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    
    if (!(_annCoor.latitude == 0.0 && _annCoor.longitude == 0.0)) {
        _callOutVC.view.alpha =1.0;
        CGPoint point = [_mapView convertCoordinate:_annCoor toPointToView:_mapView];
        _callOutVC.view.center = point;
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市净蚤,隨后出現(xiàn)的幾起案子钥组,更是在濱河造成了極大的恐慌,老刑警劉巖今瀑,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件程梦,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡橘荠,警方通過(guò)查閱死者的電腦和手機(jī)屿附,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)哥童,“玉大人挺份,你說(shuō)我怎么就攤上這事≈福” “怎么了匀泊?”我有些...
    開封第一講書人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵影暴,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我探赫,道長(zhǎng)型宙,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任伦吠,我火速辦了婚禮妆兑,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘毛仪。我一直安慰自己搁嗓,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開白布箱靴。 她就那樣靜靜地躺著腺逛,像睡著了一般。 火紅的嫁衣襯著肌膚如雪衡怀。 梳的紋絲不亂的頭發(fā)上棍矛,一...
    開封第一講書人閱讀 51,718評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音抛杨,去河邊找鬼够委。 笑死,一個(gè)胖子當(dāng)著我的面吹牛怖现,可吹牛的內(nèi)容都是我干的茁帽。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼屈嗤,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼潘拨!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起饶号,我...
    開封第一講書人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤铁追,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后讨韭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體脂信,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡癣蟋,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年透硝,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片疯搅。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡濒生,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出幔欧,到底是詐尸還是另有隱情罪治,我是刑警寧澤丽声,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站觉义,受9級(jí)特大地震影響雁社,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜晒骇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一霉撵、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧洪囤,春花似錦徒坡、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至剥啤,卻和暖如春锦溪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背府怯。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工海洼, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人富腊。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓坏逢,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親赘被。 傳聞我的和親對(duì)象是個(gè)殘疾皇子是整,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

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