ios開發--高德地圖SDK使用簡介

來源:互聯網
上載者:User

標籤:

高德LBS開放平台將高德最專業的定位、地圖、搜尋、導航等能力,以API、SDK等形式向廣大開發人員免費開放。本章節我們來簡單學習一下如何使用它的定位及地圖SDK。

一、相關架構及環境配置
  • 地圖SDK

對於如何下載SDK,它的官方文檔提供了很詳細的說明,使用CocoaPods。如果你沒有安裝CocoaPods,也可以在它的官網直接下載。

接下來只需要將SDK引入工程,完成相關的環境配置即可。在它的官方文檔中有詳細說明,這裡就不重複了。

地圖SDK文檔

  • 定位SDK

高德 iOS 定位 SDK 提供了不依賴於地圖定位的定位功能,開發人員可以無地圖顯示的情境中便捷地為應用程式添加定位功能。它的定位 SDK中提供的持續定位功能與地圖功能分離。同樣我們先下載SDK。

由於定位與地圖是不同的SDK所以一定要記得設定兩次使用者Key。

另外需要特別注意的是,在官方文檔中對於 TARGETS-Build Settings-Architectures的環境配置,在定位和地圖SDK是不同的,但是大家只要設定其中一個就可以了。

定位SDK文檔

二、範例程式碼

  • 引入相關架構,並完成環境配置

在它的官方文檔中對於需要什麼樣的架構有詳細的說明,大家根據文檔添加。

最後根據文檔我們需要設定info.plist檔案。

  • 在AppDelegate.m檔案中完成apiKey的設定
  1. #import <MAMapKit/MAMapKit.h>//地圖SDK標頭檔
  2. #import <AMapLocationKit/AMapLocationKit.h>//定位SDK標頭檔
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. [MAMapServices sharedServices].apiKey = @"990c9f469d381bd72a6915b3d0c829a5";//地圖SDK
  3. [AMapLocationServices sharedServices].apiKey [email protected]"990c9f469d381bd72a6915b3d0c829a5";//定位SDK
  4. return YES;
  5. }
  • 在viewController.m檔案中引入所需屬性,並完成懶載入
  1. #import <MAMapKit/MAMapKit.h>
  2. #import <AMapLocationKit/AMapLocationKit.h>
  1. @interface ViewController ()<MAMapViewDelegate,AMapLocationManagerDelegate>
  2. @property (nonatomic, strong) MAMapView *mapView;//地圖視圖
  3. @property (nonatomic, strong) AMapLocationManager *locationManager;//定位管理者
  4. @end
  1. - (MAMapView *)mapView{
  2. if (!_mapView) {
  3. _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
  4. _mapView.delegate = self;
  5. _mapView.mapType = MAMapTypeStandard;//設定地圖類型
  6. _mapView.showTraffic= YES;//是否顯示交通圖
  7. [self.locationManager startUpdatingLocation];//開始定位
  8. [_mapView setUserTrackingMode: MAUserTrackingModeFollow animated:YES];//定位以後改變地圖的圖層顯示。
  9. [self.view addSubview:_mapView];
  10. }
  11. return _mapView;
  12. }
  13. - (AMapLocationManager *)locationManager{
  14. if (!_locationManager) {
  15. _locationManager = [[AMapLocationManager alloc] init];
  16. _locationManager.delegate = self;
  17. }
  18. return _locationManager;
  19. }
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. [self locationManager];
  23. [self mapView];
  24. }
  • 完成定位和“圖釘”功能
  1. //AMapLocationManager代理方法位置更新以後回調。
  2. - (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
  3. {
  4. NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
  5. }
  6. -(void) viewDidAppear:(BOOL)animated
  7. {
  8. [super viewDidAppear:animated];
  9. MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
  10. pointAnnotation.coordinate = CLLocationCoordinate2DMake(31.982527, 118.735046);
  11. pointAnnotation.title = @"宏創科技";
  12. pointAnnotation.subtitle = @"國家廣告產業園XXX";
  13. [self.mapView addAnnotation:pointAnnotation];
  14. }
  15. //MAMapView代理方法,用來設定圖釘
  16. - (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id )annotation
  17. {
  18. if ([annotation isKindOfClass:[MAPointAnnotation class]])
  19. {
  20. static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
  21. MAPinAnnotationView*annotationView = (MAPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
  22. if (annotationView == nil)
  23. {
  24. annotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
  25. }
  26. annotationView.canShowCallout= YES; //設定氣泡可以彈出,預設為NO
  27. annotationView.animatesDrop = YES; //設定標註動畫顯示,預設為NO
  28. annotationView.draggable = YES; //設定標註可以拖動,預設為NO
  29. annotationView.pinColor = MAPinAnnotationColorPurple;
  30. return annotationView;
  31. }
  32. return nil;
  33. }

ios開發--高德地圖SDK使用簡介

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.