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


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

一、相關框架及環境配置

  • 地圖SDK

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

QQ20160330-0QQ20160330-1

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

QQ20160330-2

地圖SDK文檔

  • 定位SDK

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

QQ20160330-3

QQ20160330-4

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

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

定位SDK文檔

二、示例代碼

IMG_1059

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

QQ20160330-5

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

最后根據文檔我們需要設置info.plist文件。

QQ20160330-7

  • 在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 =@"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. }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM