高德LBS開放平台將高德最專業的定位、地圖、搜索、導航等能力,以API、SDK等形式向廣大開發者免費開放。本章節我們來簡單學習一下如何使用它的定位及地圖SDK。
一、相關框架及環境配置
- 地圖SDK
對於如何下載SDK,它的官方文檔提供了很詳細的說明,使用CocoaPods。如果你沒有安裝CocoaPods,也可以在它的官網直接下載。
接下來只需要將SDK引入工程,完成相關的環境配置即可。在它的官方文檔中有詳細說明,這里就不重復了。
- 定位SDK
高德 iOS 定位 SDK 提供了不依賴於地圖定位的定位功能,開發者可以無地圖顯示的場景中便捷地為應用程序添加定位功能。它的定位 SDK中提供的持續定位功能與地圖功能分離。同樣我們先下載SDK。
由於定位與地圖是不同的SDK所以一定要記得設置兩次用戶Key。
另外需要特別注意的是,在官方文檔中對於 TARGETS-Build Settings-Architectures的環境配置,在定位和地圖SDK是不同的,但是大家只要設置其中一個就可以了。
二、示例代碼
- 引入相關框架,並完成環境配置
在它的官方文檔中對於需要什么樣的框架有詳細的說明,大家根據文檔添加。
最后根據文檔我們需要設置info.plist文件。
- 在AppDelegate.m文件中完成apiKey的設置
- #import <MAMapKit/MAMapKit.h>//地圖SDK頭文件
- #import <AMapLocationKit/AMapLocationKit.h>//定位SDK頭文件
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- [MAMapServices sharedServices].apiKey = @"990c9f469d381bd72a6915b3d0c829a5";//地圖SDK
- [AMapLocationServices sharedServices].apiKey =@"990c9f469d381bd72a6915b3d0c829a5";//定位SDK
- return YES;
- }
- 在viewController.m文件中引入所需屬性,並完成懶加載
- #import <MAMapKit/MAMapKit.h>
- #import <AMapLocationKit/AMapLocationKit.h>
- @interface ViewController ()<MAMapViewDelegate,AMapLocationManagerDelegate>
- @property (nonatomic, strong) MAMapView *mapView;//地圖視圖
- @property (nonatomic, strong) AMapLocationManager *locationManager;//定位管理者
- @end
- - (MAMapView *)mapView{
- if (!_mapView) {
- _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
- _mapView.delegate = self;
- _mapView.mapType = MAMapTypeStandard;//設置地圖類型
- _mapView.showTraffic= YES;//是否顯示交通圖
- [self.locationManager startUpdatingLocation];//開始定位
- [_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:YES];//定位以后改變地圖的圖層顯示。
- [self.view addSubview:_mapView];
- }
- return _mapView;
- }
- - (AMapLocationManager *)locationManager{
- if (!_locationManager) {
- _locationManager = [[AMapLocationManager alloc] init];
- _locationManager.delegate = self;
- }
- return _locationManager;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self locationManager];
- [self mapView];
- }
- 完成定位和“大頭針”功能
- //AMapLocationManager代理方法位置更新以后回調。
- - (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
- {
- NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
- }
- -(void) viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
- pointAnnotation.coordinate = CLLocationCoordinate2DMake(31.982527, 118.735046);
- pointAnnotation.title = @"宏創科技";
- pointAnnotation.subtitle = @"國家廣告產業園XXX";
- [self.mapView addAnnotation:pointAnnotation];
- }
- //MAMapView代理方法,用來設置大頭針
- - (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id )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];
- }
- annotationView.canShowCallout= YES; //設置氣泡可以彈出,默認為NO
- annotationView.animatesDrop = YES; //設置標注動畫顯示,默認為NO
- annotationView.draggable = YES; //設置標注可以拖動,默認為NO
- annotationView.pinColor = MAPinAnnotationColorPurple;
- return annotationView;
- }
- return nil;
- }