iOS 高德地圖SDK集成初試

前言

入職新公司后接手的新項(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。

創(chuà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)類似效果圖如下:

折線、標(biāo)記點(diǎn)
圓、標(biāo)記點(diǎn)

相關(guān)參考

高德地圖iOS SDK

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

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

  • 1、通過CocoaPods安裝項(xiàng)目名稱項(xiàng)目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地?cái)?shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,211評論 3 119
  • 今天是我特快樂的一下午,因?yàn)槲乙匚夷棠碳彝鎯毫恕? 每次回我奶奶家的時(shí)候我都會干的第一件事, 是看我...
    薛易偉閱讀 200評論 0 1
  • 01 圣誕前夜,我從一場追逐中筋疲力盡地醒來,碩風(fēng)和葉和雪狼王在我的夢里廝殺不停。 屋外寂靜一片,床邊的手機(jī)無聲地...
    提拉米蘇1975閱讀 423評論 2 5
  • 今天然仔生日,大了好多。
    煙澀寒閱讀 145評論 0 0

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