iOS - 高德地圖API簡單開發(一)
阿新 • • 發佈:2019-02-08
最近看了看高德地圖的開發平臺以及它們的API,奇怪的是一直沒找到語音API,這個問題放到以後再說吧。
對於高德地圖的開發平臺,它們提供的說明文件還算健全,而且由於API是國人開發的,所以所有介面都有中文的介面說明,很是容易理解。只不過,在載入它們的第三方庫時,總是一個功能一個功能的載入,沒有像百度那樣,一下子都給了出來(開始我以為我的cocoapods壞了.......)。
高德開放平臺:http://lbs.amap.com/
做了一個簡單的demo,包括地圖展示,定位和虛擬導航,具體程式碼如下:
首先在AppDelegate裡配置了高德開發這的鑰匙:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [AMapServices sharedServices].apiKey = @"93c5496bf2b16137226af9170498045b";//這個鑰匙key需要去高德開放平臺去申請 return YES; }
然後在rootviewcontroller的view裡邊,我添加了兩個圖層View,一個是地圖展示View,另一個是虛擬導航View
// // ViewController.m // FairyFishMap // // Created by jinhui005 on 16/9/19. // Copyright © 2016年 yhl. All rights reserved. // #import "ViewController.h" #import <MAMapKit/MAMapKit.h> #import <UIKit/UIKit.h> #import <AMapFoundationKit/AMapFoundationKit.h> #import <AMapLocationKit/AMapLocationKit.h> #import <AMapNaviKit/AMapNaviKit.h> @interface ViewController () <MAMapViewDelegate, AMapLocationManagerDelegate, AMapNaviDriveManagerDelegate, AMapNaviDriveViewDelegate> @property (nonatomic, strong) MAMapView *mapView; @property (nonatomic, strong) AMapLocationManager *locationManager; @property (nonatomic, strong) UIButton *btn; @property (nonatomic, strong) AMapNaviDriveManager *driveManager; @property (nonatomic, strong) AMapNaviDriveView *driveView; @property (nonatomic, strong) AMapNaviPoint *startPoint; @property (nonatomic, strong) AMapNaviPoint *endPoint; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.mapView = [[MAMapView alloc] initWithFrame:self.view.frame]; self.mapView.delegate = self; self.mapView.showsUserLocation = YES; self.mapView.userTrackingMode = MAUserTrackingModeFollow; [self.view addSubview:self.mapView]; self.locationManager = [[AMapLocationManager alloc] init]; self.locationManager.delegate = self; [self.locationManager startUpdatingLocation]; [self.locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters]; // 帶逆地理資訊的一次定位(返回座標和地址資訊) self.locationManager.locationTimeout =2; // 定位超時時間,最低2s,此處設定為2s self.locationManager.reGeocodeTimeout = 2; // 逆地理請求超時時間,最低2s,此處設定為2s self.btn = [[UIButton alloc] init]; self.btn.frame = CGRectMake(20, 50, 90, 40); [self.btn setTitle:@"導航" forState:UIControlStateNormal]; [self.btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; [self.btn addTarget:self action:@selector(beginRouting) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.btn]; // [self showMap]; // [self controlEx]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // [self clipOneAnnnotation]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)showMap { self.mapView.showTraffic = NO; self.mapView.mapType = MAMapTypeStandard; } - (void)controlEx { self.mapView.logoCenter = CGPointMake(50, 450); self.mapView.showsCompass = NO; self.mapView.compassOrigin = CGPointMake(self.mapView.compassOrigin.x, 22); self.mapView.showsScale = NO; self.mapView.scaleOrigin = CGPointMake(self.mapView.compassOrigin.x, 22); } - (void)clipOneAnnnotation { MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init]; pointAnnotation.coordinate = CLLocationCoordinate2DMake(39.989631, 116.481018); pointAnnotation.title = @"方恆國際"; pointAnnotation.subtitle = @"阜通東大街6號"; [self.mapView addAnnotation:pointAnnotation]; } - (void)beginRouting { self.startPoint = [AMapNaviPoint locationWithLatitude:39.993135 longitude:116.474175]; self.endPoint = [AMapNaviPoint locationWithLatitude:39.908791 longitude:116.321257]; self.driveManager = [[AMapNaviDriveManager alloc] init]; self.driveManager.delegate = self; self.driveView = [[AMapNaviDriveView alloc] init]; self.driveView.frame = self.view.frame; self.driveView.delegate = self; [self.view addSubview:self.driveView]; [self.driveManager addDataRepresentative:self.driveView]; [self.driveManager calculateDriveRouteWithStartPoints:@[self.startPoint] endPoints:@[self.endPoint] wayPoints:nil drivingStrategy:AMapNaviDrivingStrategyDefault]; } #pragma mark - MAMapViewDelegate - (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)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; } - (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation { } #pragma mark - AMapLocationManagerDelegate - (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location { } #pragma mark - AMapNaviDriveManagerDelegate - (void)driveManagerOnCalculateRouteSuccess:(AMapNaviDriveManager *)driveManager { [driveManager startEmulatorNavi];//開始模擬導航 } #pragma mark - AMapNaviDriveViewDelegate @end
我在最後沒有做導航完畢的回撥相應,只是想粗略的看一看高德的API。
其實高德的API都很好理解,就比如下邊的行車導航管理器的代理方法:
每一個回撥方法都說明的特別清楚。
附上兩張虛擬導航的截圖:
最後附帶一個Podfile檔案(包括地圖的展示/定位/導航 API):
target 'FairyFishMap' do
pod 'AMap3DMap'
pod 'AMapLocation'
pod 'AMapNavi'
end