1. 程式人生 > >【iOS】高德地圖MAMapKit的使用:地圖顯示、新增大頭針、導航、定位功能介紹

【iOS】高德地圖MAMapKit的使用:地圖顯示、新增大頭針、導航、定位功能介紹

4、

引入高德地圖依賴系統庫檔案:


說明:

1.備註中,2D表示使用2D柵格地圖需要的系統檔案,3D表示使用3D向量地圖需要的系統檔案、Search表示使用搜索庫需要的系統檔案,3D(V3.X.X)表示3D向量地圖V3.0.0以後版本需要新增的庫。

2.SystemConfiguration.framework、CoreTelephonySecurity.framework、Security.framework 是為了統計app資訊使用。

3.iOS9後,需要把libz.dylib、libstdc++6.09.dylib、libc++.dylib替換成libz.tbd、libstdc++6.09.tbd、libc++.tbd。

引入系統庫的操作如下:

左側目錄中選中工程名,在TARGETS->Build Settings-> Link Binary With Libaries中點選“+”按鈕,在彈出的視窗中查詢並選擇所需的庫(見下表),單擊“Add”按鈕,將庫檔案新增到工程中。


三、地圖顯示

1、

配置使用者Key:在使用地圖 SDK 時,需要對應用做 Key 機制驗證,在地圖初始化之前新增如下示例程式碼,配置之前在官網上申請的 Key:如果地圖顯示不出來,請檢查key設定的是否正確

在 appDelegate.m的方法 :

- (BOOL)application:(UIApplication

*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.、

             中設定key:

       [AMapServicessharedServices].apiKey = @“申請的key”;//最新的SDK使用AMapServices或

       [MAMapServices sharedServices].apiKey = @"使用者Key";//比較老的SDK使用

returnYES;

}

2、

(1)在想要顯示地圖的 ViewController.m 檔案中,引入 MAMapKit.h 檔案,繼承 MAMapViewDelegate 協議,並定義 MAMapView 物件,示例程式碼如下所示:

#import <ViewController.h>
#import <MAMapKit/MAMapKit.h>

@interface ViewController ()<MAMapViewDelegate>

//地圖

@property (nonatomic, strong) MAMapView *mapView;

@end

(2)在 ViewController.m 檔案相應的方法中進行地圖初始化,初始化的步驟:

1.構造 MAMapView 物件;

2.設定代理;

3.將 MAMapView 新增到 Subview 中。

//地圖初始化

self.mapView = [[MAMapViewalloc] initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight)];

_mapView.backgroundColor = [UIColorwhiteColor];

self.mapView.delegate = self;

//設定定位精度

//    _mapView.desiredAccuracy = kCLLocationAccuracyBest;

//設定定位距離

//    _mapView.distanceFilter = 5.0f;

_mapView.zoomEnabled = YES;

//普通樣式

_mapView.mapType = MAMapTypeStandard;

//地圖跟著位置移動

    [_mapViewsetUserTrackingMode:MAUserTrackingModeFollowanimated:YES];

//設定成NO表示關閉指南針;YES表示顯示指南針

_mapView.showsCompass= NO;

//設定指南針位置

_mapView.compassOrigin= CGPointMake(_mapView.compassOrigin.x, 22);

//設定成NO表示不顯示比例尺;YES表示顯示比例尺

_mapView.showsScale= NO;

//設定比例尺位置

_mapView.scaleOrigin= CGPointMake(_mapView.scaleOrigin.x, 22);

//縮放等級

    [_mapViewsetZoomLevel:16 animated:YES];

//開啟定位

_mapView.showsUserLocation = YES;

    [self.viewaddSubview:self.mapView];



當位置更新時,會進定位回撥,通過回撥函式,能獲取到定位點的經緯度座標,示例程式碼如下:

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {

   //userLocation 就是使用者當前的位置資訊,通過userLocation 可以獲取當前的經緯度資訊及詳細的地理位置資訊,方法如下:

   //建立一個經緯度點:

MAPointAnnotation *point = [[MAPointAnnotationalloc] init];

//設定點的經緯度

        point.coordinate = _currentUL.location.coordinate;

CLLocation *currentLocation = [[CLLocationalloc]initWithLatitude:_currentUL.location.coordinate.latitudelongitude:_currentUL.location.coordinate.longitude];

// 初始化編碼器

CLGeocoder *geoCoder = [[CLGeocoderalloc] init];

        [geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {

            //獲取當前城市位置資訊,其中CLPlacemark包括name、thoroughfare、subThoroughfare、locality、subLocality等詳細資訊

CLPlacemark *mark = [placemarks lastObject];

NSString *cityName = mark.locality;

//        NSLog(@"城市 - %@", cityName);

self.currentCity  = cityName;

        }];

}

檢視顯示效果: