集成iOS高德地圖

一、前奏

這里只是個(gè)人集成過(guò)程中的遇到的點(diǎn),現(xiàn)做下標(biāo)記泼差。

1,如果你需要集成涉及國(guó)外版地圖,基于HTTPS的最新版本需要設(shè)置,需要注意基于國(guó)外定位呵俏,高德的不太精確(個(gè)人測(cè)試)

? ? [self.mapView performSelector:@selector(setShowsWorldMap:) withObject:@(YES)];

這個(gè)方法后臺(tái)提供的國(guó)外地圖是第三方的堆缘,是不穩(wěn)定的,不推薦使用普碎。集成國(guó)外版地圖詳情見(jiàn):http://lbs.amap.com/dev/demo/switch-map#iOS

2吼肥,坐標(biāo)轉(zhuǎn)換

//將GPS轉(zhuǎn)成高德坐標(biāo)

CLLocationCoordinate2D amapcoord = MACoordinateConvert(CLLocationCoordinate2DMake(39.989612,116.480972),MACoordinateTypeGPS);

二、實(shí)戰(zhàn)

1麻车,創(chuàng)建地圖

#import <MAMapKit/MAMapKit.h>

@property (nonatomic, strong)MAMapView *mapView; // 添加屬性

// 添加到視圖上
_mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];

_mapView.delegate = self;

[self.view addSubview:_mapView];

2缀皱,設(shè)置中心點(diǎn)

CLLocationCoordinate2D centerPoint = CLLocationCoordinate2DMake(30.2740850000,120.1550700000);//緯度,經(jīng)度

self.mapView.centerCoordinate = centerPoint; // 或者[self.mapView setCenterCoordinate:(centerPoint) animated:YES];

3绪氛,隱藏指南針

_mapView.showsCompass = NO;

4唆鸡,隱藏比例尺

_mapView.showsScale = NO;

5,手勢(shì)控制

_mapView.zoomEnabled = YES;? ? // NO表示禁用縮放手勢(shì)枣察,YES表示開(kāi)啟

_mapView.scrollEnabled = YES;? // NO表示禁用滑動(dòng)手勢(shì)争占,YES表示開(kāi)啟

6,設(shè)置地圖語(yǔ)言(默認(rèn)中文)

_mapView.language = MAMapLanguageEn;

7序目,添加標(biāo)注(大頭針)

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

pointAnnotation1.coordinate = CLLocationCoordinate2DMake(39.907465,116.337447);

pointAnnotation1.title = @"木樨地";

pointAnnotation1.subtitle = @"北京市西城區(qū)復(fù)興門(mén)外大街29號(hào)樓三里河路";

[_mapView addAnnotation:pointAnnotation]; // 單個(gè)添加臂痕,多個(gè)添加[_mapView addAnnotations:@[pointAnnotation,pointAnnotation1,pointAnnotation2]];

8,自定義標(biāo)注(大頭針)

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

{

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

{

// 自定義標(biāo)注圖標(biāo)

static NSString *reuseIndetifier = @"annotationReuseIndetifier";

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

if (annotationView == nil)

{

annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation

reuseIdentifier:reuseIndetifier];

}

annotationView.image = [UIImage imageNamed:@"Point"]; // 換成你需要的標(biāo)注圖片

// 設(shè)置中心點(diǎn)偏移猿涨,使得標(biāo)注底部中間點(diǎn)成為經(jīng)緯度對(duì)應(yīng)點(diǎn)

annotationView.centerOffset = CGPointMake(0, -9);

annotationView.canShowCallout = YES;? ? // 設(shè)置氣泡可以彈出握童,默認(rèn)為NO

annotationView.draggable = NO;? ? ? ? // 設(shè)置標(biāo)注可以拖動(dòng),默認(rèn)為NO

return annotationView;

/* 默認(rèn)的標(biāo)注圖標(biāo)

static NSString *pointReuseIndentifier = @"pointReuseIndentifier";

MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];

if (annotationView == nil)

{

annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];

}

annotationView.image = [UIImage imageNamed:@"Point"];

annotationView.canShowCallout = YES;? // 設(shè)置氣泡可以彈出叛赚,默認(rèn)為NO

annotationView.animatesDrop = YES;? ? // 設(shè)置標(biāo)注動(dòng)畫(huà)顯示澡绩,默認(rèn)為NO

annotationView.draggable = YES;? ? ? ? // 設(shè)置標(biāo)注可以拖動(dòng),默認(rèn)為NO

annotationView.pinColor = MAPinAnnotationColorPurple;

return annotationView;

*/

}

return nil;

}

9俺附,繪制折線

- (void)drawLine{

//構(gòu)造折線數(shù)據(jù)對(duì)象

CLLocationCoordinate2D commonPolylineCoords[3];

commonPolylineCoords[0].latitude? = 39.957312;

commonPolylineCoords[0].longitude = 116.416261;

commonPolylineCoords[1].latitude? = 39.907465;

commonPolylineCoords[1].longitude = 116.337447;

commonPolylineCoords[2].latitude? = 39.991720;

commonPolylineCoords[2].longitude = 116.271057;

//構(gòu)造折線對(duì)象

MAPolyline *commonPolyline = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:3];

//在地圖上添加折線對(duì)象

[_mapView addOverlay: commonPolyline];

}

// 設(shè)置折線的樣式- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id)overlay

{

if ([overlay isKindOfClass:[MAPolyline class]])

{

MAPolylineView *polylineView = [[MAPolylineView alloc] initWithPolyline:overlay];

polylineView.lineWidth = 5.f; // 折線的寬度

polylineView.strokeColor = [UIColor redColor]; // 折線的顏色

// 端點(diǎn)類(lèi)型

polylineView.lineCap = kCGLineCapSquare;

// 連接類(lèi)型

polylineView.lineJoin = kCGLineJoinBevel;

return polylineView;

}

return nil;

}

9.給標(biāo)注點(diǎn)添加不同的圖標(biāo)

// 根據(jù)anntation生成對(duì)應(yīng)的View- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation

{

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

{

// 自定義標(biāo)注圖標(biāo)

static NSString *reuseIndetifier = @"annotationReuseIndetifier";

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

if (annotationView == nil)

{

annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation

reuseIdentifier:reuseIndetifier];

}

annotationView.image = [UIImage imageNamed:@"Point"];

if ([annotation.title isEqualToString:@"海神廟"]) {

annotationView.image = [UIImage imageNamed:@"Point"];

}

if ([annotation.title isEqualToString:@"金巴蘭海灘"]) {

annotationView.image = [UIImage imageNamed:@"pointNew"];

}

// 設(shè)置中心點(diǎn)偏移肥卡,使得標(biāo)注底部中間點(diǎn)成為經(jīng)緯度對(duì)應(yīng)點(diǎn)

annotationView.centerOffset = CGPointMake(0, -9);

annotationView.canShowCallout = YES;? ? // 設(shè)置氣泡可以彈出,默認(rèn)為NO

annotationView.draggable = NO;? ? ? ? // 設(shè)置標(biāo)注可以拖動(dòng)事镣,默認(rèn)為NO

return annotationView;

/* 默認(rèn)的標(biāo)注圖標(biāo)

static NSString *pointReuseIndentifier = @"pointReuseIndentifier";

MAPinAnnotationView *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];

if (annotationView == nil)

{

annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];

}

annotationView.image = [UIImage imageNamed:@"Point"];

annotationView.canShowCallout = YES;? // 設(shè)置氣泡可以彈出步鉴,默認(rèn)為NO

annotationView.animatesDrop = YES;? ? // 設(shè)置標(biāo)注動(dòng)畫(huà)顯示,默認(rèn)為NO

annotationView.draggable = YES;? ? ? ? // 設(shè)置標(biāo)注可以拖動(dòng),默認(rèn)為NO

annotationView.pinColor = MAPinAnnotationColorPurple;

return annotationView;

*/

}

return nil;

}

10.點(diǎn)擊標(biāo)注點(diǎn)氛琢,觸發(fā)不同的事件

通過(guò)協(xié)議方法進(jìn)行判斷

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view

根據(jù)點(diǎn)擊不同的標(biāo)注點(diǎn)數(shù)據(jù)進(jìn)行處理

MAPointAnnotation *pointAnnotation = (MAPointAnnotation *)mapView.selectedAnnotations[0];

NSLog(@"city =>%@,subtitle =>%@",pointAnnotation.title,pointAnnotation.subtitle);

例如:

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view{

NSLog(@"selectedAnnotations ==》%@",mapView.selectedAnnotations);

if (mapView.selectedAnnotations.count == 0) {

return;

}

MAPointAnnotation *pointAnnotation = (MAPointAnnotation *)mapView.selectedAnnotations[0];

NSLog(@"city =>%@,subtitle =>%@",pointAnnotation.title,pointAnnotation.subtitle);

for (int i = 0; i < self.addressArray.count; i++) {

AddressModel *model = (AddressModel *)self.addressArray[i];

if ([pointAnnotation.title isEqualToString:model.cityName]) {

// 給底部view賦值

if (self.bottomView.hidden) {

self.bottomView.hidden = NO;

}

self.bottomView.cityNameLable.text = model.cityName;

self.bottomView.cityEnglishLable.text = model.detail;

self.bottomView.avgpriceLable.text = [NSString stringWithFormat:@"人均:%d",i*100];

break;

}

}

}

11.在地圖上顯示所有標(biāo)注點(diǎn)喊递,通過(guò)協(xié)議

- (void)showAnnotations:(NSArray *)annotations animated:(BOOL)animated;

在視線內(nèi)顯示所有的標(biāo)注,會(huì)自動(dòng)計(jì)算最佳縮放比以將所有點(diǎn)顯示出來(lái)

12.默認(rèn)顯示氣泡

??? [_mapView selectAnnotation:pointAnnotation animated:YES];

13.自定義標(biāo)注點(diǎn),例如更換圖標(biāo)

在 協(xié)議的回調(diào)函數(shù)mapView:viewForAnnotation:中修改 MAAnnotationView 對(duì)應(yīng)的標(biāo)注圖片阳似。示例代碼如下:

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
??? if ([annotation isKindOfClass:[MAPointAnnotation class]])
??? {
??????? static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
??????? MAPinAnnotationView? *annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];

??????? if (annotationView == nil)
??????? {
??????????? annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
??????? }

??????? // 設(shè)置自定義圖標(biāo)
??????? annotationView.image = [UIImage imageNamed:@"price_lv5_yellow"];
???????
??????? // 設(shè)置中心點(diǎn)偏移骚勘,使得標(biāo)注底部中間點(diǎn)成為經(jīng)緯度對(duì)應(yīng)點(diǎn)
??????? annotationView.centerOffset = CGPointMake(0, -18);
???????
//??????? 如果要自定義標(biāo)注點(diǎn)圖標(biāo),一定要注釋掉下面幾行
//??????? annotationView.animatesDrop = YES;??????? //設(shè)置標(biāo)注動(dòng)畫(huà)顯示障般,默認(rèn)為NO
//??????? annotationView.draggable = YES;?????????? //設(shè)置標(biāo)注可以拖動(dòng)调鲸,默認(rèn)為NO
//??????? annotationView.pinColor = MAPinAnnotationColorPurple;

??????? return annotationView;
??? }

??? return nil;
}

一定要注釋掉下面幾行,否則自定義的圖標(biāo)不會(huì)顯示

//??????? annotationView.animatesDrop = YES;??????? //設(shè)置標(biāo)注動(dòng)畫(huà)顯示挽荡,默認(rèn)為NO

//??????? annotationView.draggable = YES;?????????? //設(shè)置標(biāo)注可以拖動(dòng)藐石,默認(rèn)為NO

//??????? annotationView.pinColor = MAPinAnnotationColorPurple;

三、備注

詳情還要看官方文檔:地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末定拟,一起剝皮案震驚了整個(gè)濱河市于微,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌青自,老刑警劉巖株依,帶你破解...
    沈念sama閱讀 221,548評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異延窜,居然都是意外死亡恋腕,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門(mén)逆瑞,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)荠藤,“玉大人,你說(shuō)我怎么就攤上這事获高」ぃ” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 167,990評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵念秧,是天一觀的道長(zhǎng)淤井。 經(jīng)常有香客問(wèn)我,道長(zhǎng)摊趾,這世上最難降的妖魔是什么币狠? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 59,618評(píng)論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮砾层,結(jié)果婚禮上总寻,老公的妹妹穿的比我還像新娘。我一直安慰自己梢为,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評(píng)論 6 397
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著铸董,像睡著了一般祟印。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上粟害,一...
    開(kāi)封第一講書(shū)人閱讀 52,246評(píng)論 1 308
  • 那天蕴忆,我揣著相機(jī)與錄音,去河邊找鬼悲幅。 笑死套鹅,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的汰具。 我是一名探鬼主播卓鹿,決...
    沈念sama閱讀 40,819評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼留荔!你這毒婦竟也來(lái)了吟孙?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,725評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤聚蝶,失蹤者是張志新(化名)和其女友劉穎杰妓,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體碘勉,經(jīng)...
    沈念sama閱讀 46,268評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡巷挥,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了验靡。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片倍宾。...
    茶點(diǎn)故事閱讀 40,488評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖晴叨,靈堂內(nèi)的尸體忽然破棺而出凿宾,到底是詐尸還是另有隱情,我是刑警寧澤兼蕊,帶...
    沈念sama閱讀 36,181評(píng)論 5 350
  • 正文 年R本政府宣布初厚,位于F島的核電站,受9級(jí)特大地震影響孙技,放射性物質(zhì)發(fā)生泄漏产禾。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評(píng)論 3 333
  • 文/蒙蒙 一牵啦、第九天 我趴在偏房一處隱蔽的房頂上張望亚情。 院中可真熱鬧,春花似錦哈雏、人聲如沸楞件。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,331評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)土浸。三九已至罪针,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間黄伊,已是汗流浹背泪酱。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,445評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留还最,地道東北人墓阀。 一個(gè)月前我還...
    沈念sama閱讀 48,897評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像拓轻,于是被迫代替她去往敵國(guó)和親斯撮。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評(píng)論 2 359

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