前言
入職新公司后接手的新項(xiàng)目使用OC開發(fā),項(xiàng)目主要功能里涉及地圖開發(fā),技術(shù)選型上使用高德地圖服務(wù),本篇博客主要記錄高德地圖SDK的集成以及相關(guān)使用。
環(huán)境配置
OC, Xcode 9.4.1
注冊
首先在高德開發(fā)平臺注冊一個(gè)賬號,注冊流程略。注冊完成后,我們需要在控制臺“我的應(yīng)用”里創(chuàng)建一個(gè)應(yīng)用,根據(jù)使用的平臺創(chuàng)建相應(yīng)的Key。

引入SDK
這里我們使用CocoaPods來管理高德地圖的SDK。在項(xiàng)目的podfile里根據(jù)需要添加以下SDK。
pod 'AMap2DMap' #高德2D地圖SDK
pod 'AMap3DMap' #高德3D地圖SDK
pod 'AMapLocation' #高德定位SDK
pod 'AMapSearch' #高德地圖搜索SDK
pod 'AMapTrack' #高德獵鷹SDK
注意:高德2D地圖和3D地圖SDK不能同時(shí)引入,其主要差別在于:2D地圖是柵格地圖,采用切片的方式顯示地圖,3D地圖為矢量地圖,采用終端繪制地圖的方式,地圖功能更加豐富。
功能使用
啟動SDK
在引入頭文件#import<AMapFoundationKit/AMapFoundationKit.h>后,調(diào)用以下方法配置高德服務(wù)的key。
[AMapServices sharedServices].apiKey =@"您的key";
顯示地圖
實(shí)例化一個(gè)地圖,添加到控制器的view上。我們讓控制器持有一個(gè)地圖屬性,在該屬性的get方法里配置地圖的功能。
- (MAMapView *)mapView {
if (!_mapView) {
_mapView = [[MAMapView alloc] initWithFrame: self.view.bounds];
// 顯示比例尺
_mapView.showsScale = NO;
// 顯示指南針
_mapView.showsCompass = NO;
// 顯示定位藍(lán)點(diǎn)
_mapView.showsUserLocation = YES;
// 用戶定位模式
_mapView.userTrackingMode = MAUserTrackingModeFollow;
// 設(shè)置縮放級別
[_mapView setZoomLevel:15];
// 設(shè)置當(dāng)前地圖的中心點(diǎn):例如默認(rèn)地圖中心顯示坐標(biāo)為(39.9088230000, 116.3974700000)
_mapView.centerCoordinate = CLLocationCoordinate2DMake(39.9088230000, 116.3974700000);
}
return _mapView;
}
繪制標(biāo)記點(diǎn)、折線、面
通過實(shí)現(xiàn)MAMapViewDelegate的相關(guān)代理方法實(shí)現(xiàn)地圖繪制。
標(biāo)記點(diǎn)
/**
* @brief 根據(jù)anntation生成對應(yīng)的View
* @param mapView 地圖View
* @param annotation 指定的標(biāo)注
* @return 生成的標(biāo)注View
*/
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
if ([annotation isKindOfClass:[MAPointAnnotation class]]) {
static NSString *reuseIndetifier = @"annotationReuseIndetifier";
MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
if (annotationView == nil) {
annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:reuseIndetifier];
}
// 設(shè)置標(biāo)記點(diǎn)的圖片
// annotationView.image = ;
// 設(shè)置中心點(diǎn)偏移,使得標(biāo)注底部中間點(diǎn)成為經(jīng)緯度對應(yīng)點(diǎn)
// annotationView.centerOffse =
return annotationView;
}
return nil;
}
標(biāo)注可以精確表示用戶需要展示的位置信息,高德地圖SDK提供的標(biāo)注功能允許用戶自定義圖標(biāo)和信息窗,同時(shí)提供了標(biāo)注的點(diǎn)擊、拖動事件的回調(diào)。SDK 提供的地圖標(biāo)注為MAAnnotation類,不同的標(biāo)記可以根據(jù)圖標(biāo)和改變信息窗的樣式和內(nèi)容加以區(qū)分。
iOS SDK提供的大頭針標(biāo)注MAPinAnnotationView,通過它可以設(shè)置大頭針顏色、是否顯示動畫、是否支持長按后拖拽大頭針改變坐標(biāo)等。
通過以下方法添加標(biāo)注。
// 1 實(shí)例化MAPointAnnotation類
MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
// 2 設(shè)置標(biāo)注點(diǎn)的坐標(biāo)
pointAnnotation.coordinate = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
// 3 調(diào)用地圖的addAnnotation方法將標(biāo)注點(diǎn)添加到地圖上
[self.mapView addAnnotation:pointAnnotation];
折線、面
/**
* @brief 根據(jù)overlay生成對應(yīng)的Renderer
* @param mapView 地圖View
* @param overlay 指定的overlay
* @return 生成的覆蓋物Renderer
*/
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
// 線
if ([overlay isKindOfClass:[MAPolyline class]]) {
MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
// 線寬
polylineRenderer.lineWidth = 3.f;
// 顏色
polylineRenderer.strokeColor = [self markColor];
return polylineRenderer;
}
// 圓形
if ([overlay isKindOfClass:[MACircle class]]) {
MACircleRenderer *circleRenderer = [[MACircleRenderer alloc] initWithCircle:overlay];
circleRenderer.lineWidth = 0;
circleRenderer.strokeColor = [[self markColor] colorWithAlphaComponent:0.2];
circleRenderer.fillColor = [[self markColor] colorWithAlphaComponent:0.2];
return circleRenderer;
}
return nil;
}
折線類為 MAPolyline,由一組經(jīng)緯度坐標(biāo)組成,并以有序序列形式建立一系列的線段。iOS SDK支持在3D矢量地圖上繪制帶箭頭或有紋理等樣式的折線,同時(shí)可設(shè)置折線端點(diǎn)和連接點(diǎn)的類型,以滿足各種繪制線的場景。
//構(gòu)造折線數(shù)據(jù)對象
CLLocationCoordinate2D commonPolylineCoords[4];
commonPolylineCoords[0].latitude = 39.832136;
commonPolylineCoords[0].longitude = 116.34095;
commonPolylineCoords[1].latitude = 39.832136;
commonPolylineCoords[1].longitude = 116.42095;
commonPolylineCoords[2].latitude = 39.902136;
commonPolylineCoords[2].longitude = 116.42095;
commonPolylineCoords[3].latitude = 39.902136;
commonPolylineCoords[3].longitude = 116.44095;
//構(gòu)造折線對象
MAPolyline *commonPolyline = [MAPolyline polylineWithCoordinates:commonPolylineCoords count:4];
//在地圖上添加折線對象
[_mapView addOverlay: commonPolyline];
通過MACircle類繪制圓,圓是由中心點(diǎn)(經(jīng)緯度)和半徑(米)構(gòu)成。
//構(gòu)造圓
MACircle *circle = [MACircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(39.952136, 116.50095) radius:5000];
//在地圖上添加圓
[_mapView addOverlay: circle];
可實(shí)現(xiàn)類似效果圖如下: