最近在做基于高德地圖的定位、導航及添加大頭針的功能,特此記錄下來。。。方便剛接觸的同學參考。。。
一、申請 Key:獲取用戶Key
1.訪問 http://lbs.amap.com/console/key/,使用高德開發者賬號登陸
2.在“KEY管理”頁面點擊上方的“獲取key”按鈕,依次輸入應用名,選擇綁定的服務為“iOS平臺SDK”,輸入 Bundle Identifier(獲取方法請參考:獲取 Bundle Indentifier),如下圖所示:
獲取 Bundle ID:iOS bundle 獲取方式
方法一:
通過代碼獲取,代碼如下所示:NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
方法二:
Xcode 切換到 General 標簽,查看 Bundle Identifier,如下圖所示:
二、在地圖顯示前進行項目的配置工作:
在plist配置字段
定位權限
NSLocationAlwaysUsageDescription:一直定位
NSLocationWhenInUseUsageDescription:需要時定位
ATS設置:Https協議
三、前往高德地圖官網下載需要使用的SDK,解壓得到如圖framework:
地圖顯示是地圖 SDK 的基礎功能,是使用地圖定位、導航等他功能的載體。
首先,在lbs.amap.com/api/ios-sdk/download/頁面中根據您的需求下載庫文件并解壓,包括:
3D 矢量地圖庫,解壓后得到 MAMapKit.framework 文件。3D 矢量地圖效果優,可查看 3D 樓塊,功能全,還支持離線地圖,能幫您節省流量。目前暫不支持地圖多實例。
2D 柵格地圖庫,解壓后得到 MAMapKit.framework 文件。2D 柵格地圖庫體積小,能耗低。支持地圖多實例。
搜索庫,解壓后得到 AMapSearchKit.framework 文件。搜索庫功能包含:POI 查詢、路徑規劃、地理編碼和逆地理編碼、公交查詢以及輸入提示語查詢
注意:3D矢量地圖和2D柵格地圖只能選擇一個使用,接口類似,但是地圖顯示樣式不同。
1、新建工程:新建一個 GDMap_DEMO Application工程,如下圖所示:、
2、配置工程:引入地圖庫
左側目錄中選中工程名,在 TARGETS->Build Phases-> Link Binary With Libaries 中點擊 “+” 按鈕,在彈出的窗口中點擊 “Add Other” 按鈕,選擇解壓后的 MAMapKit.framework 文件添加到工程中。
3、引入AMap.bundle資源文件,AMap.bundle 資源文件中存儲了定位、默認大頭針標注視圖等圖片,可利用這些資源圖片進行開發,
左側目錄中選中工程名,在右鍵菜單中選擇 Add Files to “工程名”…,從MAMapKit.framework->Resources 文件中選擇 AMap.bundle 文件,并勾選 “Copy items if needed” 復選框,單擊 “Add” 按鈕,將資源文件添加到工程中。
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:
[AMapServices sharedServices].apiKey = @“申請的key”;//最新的SDK使用AMapServices或
[MAMapServices sharedServices].apiKey = @"用戶Key";//比較老的SDK使用
return YES;
}
```
2、
1)在想要顯示地圖的 ViewController.m 文件中,引入 MAMapKit.h 文件,繼承 MAMapViewDelegate 協議,并定義 MAMapView 對象,示例代碼如下所示:
```
#import#import@interface ViewController ()//地圖
@property (nonatomic, strong) MAMapView *mapView;
@end
```
2)在 ViewController.m 文件相應的方法中進行地圖初始化,初始化的步驟:
(1).構造 MAMapView 對象;(2).設置代理;(3).將 MAMapView 添加到 Subview 中
//地圖初始化
```
self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight)];
_mapView.backgroundColor = [UIColor whiteColor];
self.mapView.delegate = self;
//設置定位精度
//? ? _mapView.desiredAccuracy = kCLLocationAccuracyBest;
//設置定位距離
//? ? _mapView.distanceFilter = 5.0f;
_mapView.zoomEnabled = YES;
//普通樣式
_mapView.mapType = MAMapTypeStandard;
//地圖跟著位置移動
[_mapView setUserTrackingMode:MAUserTrackingModeFollow animated: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);
//縮放等級
[_mapView setZoomLevel:16 animated:YES];
//開啟定位
_mapView.showsUserLocation = YES;
[self.view addSubview:self.mapView];
```
當位置更新時,會進定位回調,通過回調函數,能獲取到定位點的經緯度坐標,示例代碼如下:
```
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation {
//userLocation 就是用戶當前的位置信息,通過userLocation 可以獲取當前的經緯度信息及詳細的地理位置信息,方法如下:
//創建一個經緯度點:
MAPointAnnotation *point = [[MAPointAnnotation alloc] init];
//設置點的經緯度
point.coordinate = _currentUL.location.coordinate;
CLLocation *currentLocation = [[CLLocation alloc]initWithLatitude:_currentUL.location.coordinate.latitude longitude:_currentUL.location.coordinate.longitude];
// 初始化編碼器
CLGeocoder *geoCoder = [[CLGeocoder alloc] 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;
}];
}
```
查看顯示效果:
四、添加大頭針:
代碼:
//創建坐標
```
CLLocationCoordinate2D coor ;
coor.latitude = @“39.33232132323”;
coor.longitude = @“116.23423423423”;
MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
pointAnnotation.coordinate = coor;
//設置地圖的定位中心點坐標
self.mapView.centerCoordinate = coor;
//將點添加到地圖上,即所謂的大頭針
[self.mapView addAnnotation:pointAnnotation];
添加了大頭針,會進入mapview的代理方法:在代理方法中可以設置大頭針的顯示圖片,及點擊大頭針彈出的氣泡視圖
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id)annotation {
//大頭針標注
if ([annotation isKindOfClass:[MAPointAnnotation class]]) {//判斷是否是自己的定位氣泡,如果是自己的定位氣泡,不做任何設置,顯示為藍點,如果不是自己的定位氣泡,比如大頭針就會進入
static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
MAAnnotationView*annotationView = (MAAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
if (annotationView == nil) {
annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
}
annotationView.frame = CGRectMake(0, 0, 100, 100);
annotationView.canShowCallout= YES;? ? ? //設置氣泡可以彈出,默認為NO
//annotationView.animatesDrop = YES;? ? ? ? //設置標注動畫顯示,默認為NO
annotationView.draggable = YES;? ? ? ? ? //設置標注可以拖動,默認為NO
//? ? ? ? annotationView.pinColor = MAPinAnnotationColorPurple;
//設置大頭針顯示的圖片
annotationView.image = [UIImage imageNamed:@"point"];
//點擊大頭針顯示的左邊的視圖
UIImageView *imageV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"backImage"]];
annotationView.leftCalloutAccessoryView = imageV;
//點擊大頭針顯示的右邊視圖
UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 50)];
rightButton.backgroundColor = [UIColor grayColor];
[rightButton setTitle:@"導航" forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = rightButton;
//? ? ? ? annotationView.image = [UIImage imageNamed:@"redPin"];
return annotationView;
}
return nil;
}
```
查看效果圖:
五、后續更新導航功能,請繼續關注。。。