標籤:
高德LBS開放平台將高德最專業的定位、地圖、搜尋、導航等能力,以API、SDK等形式向廣大開發人員免費開放。本章節我們來簡單學習一下如何使用它的定位及地圖SDK。
一、相關架構及環境配置
對於如何下載SDK,它的官方文檔提供了很詳細的說明,使用CocoaPods。如果你沒有安裝CocoaPods,也可以在它的官網直接下載。
接下來只需要將SDK引入工程,完成相關的環境配置即可。在它的官方文檔中有詳細說明,這裡就不重複了。
地圖SDK文檔
高德 iOS 定位 SDK 提供了不依賴於地圖定位的定位功能,開發人員可以無地圖顯示的情境中便捷地為應用程式添加定位功能。它的定位 SDK中提供的持續定位功能與地圖功能分離。同樣我們先下載SDK。
由於定位與地圖是不同的SDK所以一定要記得設定兩次使用者Key。
另外需要特別注意的是,在官方文檔中對於 TARGETS-Build Settings-Architectures的環境配置,在定位和地圖SDK是不同的,但是大家只要設定其中一個就可以了。
定位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 [email protected]"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;
- }
ios開發--高德地圖SDK使用簡介