IOS 高德地图导航
2016-06-28 16:18
851 查看
引言
高德地图导航包还是浅显易懂,笔者在这里做点总结,实际操作一遍。导航分为模拟导航和实时导航两种,两种导航都包括语音提示、停止导航、暂停或继续导航功能。通过模拟导航,用户可预先了解出行路线,直观掌握沿途每一个特别路口的交通状况,让出行更从容。
算路成功后就可在导航视图或HUD视图下开始导航了,如下图所示:
1.配置工程
笔者是使用cocoapods自动部署导航SDK,如果更新了cocoa pods,现在编写podfile文件有点不一样。以下实例:
platform :ios, ‘8.0’
use_frameworks!
target ‘约车’ do
pod ‘AMap3DMap’
pod ‘AMapSearch’
pod ‘AMapLocation’
pod ‘AMapNavi’
end
2.配置key
导入工程之后,我们需要在高德地图平台申请应用的key作为唯一标识,可参见官方文档,关于配置key笔者在此就不赘述。
3.实施
配置好之后,在viewController中导入
#import <AMapNaviKit/AMapNaviKit.h> #import <AMapLocationKit/AMapLocationKit.h>
我们以标准导航视图为例:
(1)
//配置地图 - (void)configMap { locationManager = [[AMapLocationManager alloc] init]; locationManager.delegate = self; [locationManager startUpdatingLocation]; }
//获取当前位置经纬度 - (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location { if (!flag) { myPosition = location.coordinate; [self routeCal]; } flag = YES; }
//设置驾车路线 - (void)routeCal { //导航起始坐标位置 AMapNaviPoint *startPoint = [AMapNaviPoint locationWithLatitude:myPosition.latitude longitude:myPosition.longitude]; //导航终点坐标位置 AMapNaviPoint *endPoint = [AMapNaviPoint locationWithLatitude:myPosition.latitude+1 longitude:myPosition.longitude+1]; NSArray *startPoints = @[startPoint]; NSArray *endPoints = @[endPoint]; //驾车路径规划(未设置途经点、导航策略为速度优先) [self.driveManager calculateDriveRouteWithStartPoints:startPoints endPoints:endPoints wayPoints:nil drivingStrategy:0]; }
获取自身位置不是必须的,我们可以使用高德地图导航SDK中的另外一种方法规划驾车路径,不带起点的路径规划(以自身位置为七点)。
- (BOOL)calculateDriveRouteWithEndPoints:(NSArray<AMapNaviPoint *> *)endPoints wayPoints:(nullable NSArray<AMapNaviPoint *> *)wayPoints drivingStrategy:(AMapNaviDrivingStrategy)strategy;
//初始化标准导航视图 - (void)initNaviDriveView { if (self.naviDriveView == nil) { self.naviDriveView = [[AMapNaviDriveView alloc] initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame))]; self.naviDriveView.delegate = self; //导航界面模式 self.naviDriveView.showMode = AMapNaviDriveViewShowModeCarPositionLocked; //导航界面跟随模式 self.naviDriveView.trackingMode = AMapNaviViewTrackingModeCarNorth; } }
/*标准导航视图回调方法*/ - (void)driveManagerOnCalculateRouteSuccess:(AMapNaviDriveManager *)driveManager { //将naviDriveView添加到AMapNaviDriveManager中 /** * 在这里可以修改用哪个模式 标准或者HUD */ [self.driveManager addDataRepresentative:self.naviDriveView]; //将导航视图添加到视图层级中 [self.view addSubview:self.naviDriveView]; //开始实时导航 [self.driveManager startGPSNavi]; } - (void)initDriveManager { if (self.driveManager == nil) { self.driveManager = [[AMapNaviDriveManager alloc] init]; [self.driveManager setDelegate:self]; //开启智能播报,包括路面电子眼,路障等等。。。 [self.driveManager setDetectedMode:AMapNaviDetectedModeCameraAndSpecialRoad]; } } //语音播报 -(void)driveManager:(AMapNaviDriveManager *)driveManager playNaviSoundString:(NSString *)soundString soundStringType:(AMapNaviSoundType)soundStringType { if (soundStringType == AMapNaviSoundTypePassedReminder) { AudioServicesPlaySystemSound(1009);//播放系统“叮叮”提示音 } else { [[SpeechSynthesizer sharedSpeechSynthesizer] speakString:soundString]; } }
SpeechSynthesizer 语音播报文件可参考Demo
AudioServicesPlaySystemSound(1009);//播放系统“叮叮”提示音 需要导入头文件<AudioToolbox/AudioToolbox.h>
至此,标准导航视图创建成功。如果你想使用HUD导航视图,只需要
//初始化HUD导航视图 - (void)initHudView { if (self.hudView == nil) { self.hudView = [[AMapNaviHUDView alloc] initWithFrame:CGRectMake(0, 64, 375, 667)]; self.hudView.delegate = self; self.hudView.isMirror = NO;//是否开启镜像 } }
只需要在下面这个回调方法中将标准导航视图设置成HUD视图即可
- (void)driveManagerOnCalculateRouteSuccess:(AMapNaviDriveManager *)driveManager
以上来源于高德地图导航SDK的一些归纳
相关文章推荐
- Android ADT 23.0.0无法更新到23.0.2问题解决方案
- SDKMAN:轻松管理多个软件开发套件 (SDK) 的命令行工具
- Parse正式发布开源PHP SDK
- 基于Android SDK-在64位Linux中使用需要注意的问题
- Android编程之SDK安装组件的离线安装方法分享
- C#调用Nero SDK刻录光盘的方法
- 使用科大讯飞语音SDK实现文字在线合成语音
- 在Android Studio中使用BaiduMap SDK实时获取当地位置信息
- android 微信 sdk api调用不成功解决方案
- Android开发的IDE、ADT、SDK、JDK、NDK等名词解释
- 基于Android设计模式之--SDK源码之策略模式的详解
- 更新android SDK 失败的解决方法
- Android在线更新SDK的方法(使用国内镜像)
- android开发环境搭建详解(eclipse + android sdk)
- Android SDK在线更新镜像服务器大全
- Android SDK三种更新失败及其解决方法
- 解决Android SDK下载和更新失败的方法详解
- Android SDK中的Support兼容包详解
- VC中SDK与MFC的区别浅析
- Win32应用程序(SDK)设计原理详解