继上一篇博客之后的导航功能。

线路规划主要代码:

//进行路径规划[self.driveManager calculateDriveRouteWithStartPoints:@[self.startPoint]endPoints:@[self.endPoint]wayPoints:nildrivingStrategy:AMapNaviDrivingStrategySingleDefault];

接下来叙述项目配置:
1、去高德开放平台下载对应的导航AMapNaviKit.framework并解压

2、将AMapNaviKit.framework拖入工程,记得选择:

3、添加导航需要的资源文件  AMapNavi.bundle,如图:

4、下面开始代码部分:(自定义控制器,将下面的代码复制进控制器即可,只需要在上个控制器设置好CLLocationCoordinate2D coor、MAUserLocation *currentUL即可进行导航功能)
在导航控制器.h文件导入头文件#import <AMapNaviKit/AMapNaviKit.h>并添加几个属性:

@property (nonatomic, strong) AMapNaviDriveManager *driveManager;@property (nonatomic, strong) AMapNaviDriveView *driveView;@property (nonatomic, strong) MAUserLocation *currentUL;//导航起始位置,即用户当前的位置@property (nonatomic, assign) CLLocationCoordinate2D coor;//导航终点位置

导航控制器.m文件:

#import "SpeechSynthesizer.h"
#import "MoreMenuView.h"@interface GPSNaviViewController ()<AMapNaviDriveManagerDelegate, AMapNaviDriveViewDelegate, MoreMenuViewDelegate>@property (nonatomic, strong) AMapNaviPoint *startPoint;
@property (nonatomic, strong) AMapNaviPoint *endPoint;@property (nonatomic, strong) MoreMenuView *moreMenu;@end@implementation GPSNaviViewController#pragma mark - Life Cycle- (void)viewDidLoad
{[super viewDidLoad];//    [self setNavigationBackItem];[self initProperties];[self initDriveView];[self initDriveManager];[self initMoreMenu];
}- (void)viewWillAppear:(BOOL)animated
{[super viewWillAppear:animated];self.navigationController.navigationBarHidden = YES;self.navigationController.toolbarHidden = YES;
}- (void)viewDidAppear:(BOOL)animated
{[super viewDidAppear:animated];[self calculateRoute];
}- (void)viewWillLayoutSubviews
{UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0){interfaceOrientation = self.interfaceOrientation;}if (UIInterfaceOrientationIsPortrait(interfaceOrientation)){[self.driveView setIsLandscape:NO];}else if (UIInterfaceOrientationIsLandscape(interfaceOrientation)){[self.driveView setIsLandscape:YES];}
}- (BOOL)prefersStatusBarHidden
{return YES;
}#pragma mark - Initalization- (void)initProperties
{//设置导航的起点和终点self.startPoint = [AMapNaviPoint locationWithLatitude:_currentUL.coordinate.latitude longitude:_currentUL.coordinate.longitude];self.endPoint   = [AMapNaviPoint locationWithLatitude:_coor.latitude longitude:_coor.longitude];
}- (void)initDriveManager
{if (self.driveManager == nil){self.driveManager = [[AMapNaviDriveManager alloc] init];[self.driveManager setDelegate:self];[self.driveManager setAllowsBackgroundLocationUpdates:YES];[self.driveManager setPausesLocationUpdatesAutomatically:NO];//将driveView添加为导航数据的Representative,使其可以接收到导航诱导数据[self.driveManager addDataRepresentative:self.driveView];}
}- (void)initDriveView
{if (self.driveView == nil){self.driveView = [[AMapNaviDriveView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];self.driveView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;[self.driveView setDelegate:self];[self.view addSubview:self.driveView];}
}- (void)initMoreMenu
{if (self.moreMenu == nil){self.moreMenu = [[MoreMenuView alloc] init];self.moreMenu.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;[self.moreMenu setDelegate:self];}
}#pragma mark - Route Plan- (void)calculateRoute
{//进行路径规划[self.driveManager calculateDriveRouteWithStartPoints:@[self.startPoint]endPoints:@[self.endPoint]wayPoints:nildrivingStrategy:AMapNaviDrivingStrategySingleDefault];
}#pragma mark - AMapNaviDriveManager Delegate- (void)driveManager:(AMapNaviDriveManager *)driveManager error:(NSError *)error
{NSLog(@"error:{%ld - %@}", (long)error.code, error.localizedDescription);
}- (void)driveManagerOnCalculateRouteSuccess:(AMapNaviDriveManager *)driveManager
{NSLog(@"onCalculateRouteSuccess");//算路成功后开始GPS导航[self.driveManager startGPSNavi];
}- (void)driveManager:(AMapNaviDriveManager *)driveManager onCalculateRouteFailure:(NSError *)error
{NSLog(@"onCalculateRouteFailure:{%ld - %@}", (long)error.code, error.localizedDescription);
}- (void)driveManager:(AMapNaviDriveManager *)driveManager didStartNavi:(AMapNaviMode)naviMode
{NSLog(@"didStartNavi");
}- (void)driveManagerNeedRecalculateRouteForYaw:(AMapNaviDriveManager *)driveManager
{NSLog(@"needRecalculateRouteForYaw");
}- (void)driveManagerNeedRecalculateRouteForTrafficJam:(AMapNaviDriveManager *)driveManager
{NSLog(@"needRecalculateRouteForTrafficJam");
}- (void)driveManager:(AMapNaviDriveManager *)driveManager onArrivedWayPoint:(int)wayPointIndex
{NSLog(@"onArrivedWayPoint:%d", wayPointIndex);
}- (void)driveManager:(AMapNaviDriveManager *)driveManager playNaviSoundString:(NSString *)soundString soundStringType:(AMapNaviSoundType)soundStringType
{NSLog(@"playNaviSoundString:{%ld:%@}", (long)soundStringType, soundString);[[SpeechSynthesizer sharedSpeechSynthesizer] speakString:soundString];
}- (void)driveManagerDidEndEmulatorNavi:(AMapNaviDriveManager *)driveManager
{NSLog(@"didEndEmulatorNavi");
}- (void)driveManagerOnArrivedDestination:(AMapNaviDriveManager *)driveManager
{NSLog(@"onArrivedDestination");
}#pragma mark - AMapNaviWalkViewDelegate- (void)driveViewCloseButtonClicked:(AMapNaviDriveView *)driveView
{//停止导航[self.driveManager stopNavi];[self.driveManager removeDataRepresentative:self.driveView];//停止语音[[SpeechSynthesizer sharedSpeechSynthesizer] stopSpeak];[self.navigationController popViewControllerAnimated:YES];
}- (void)driveViewMoreButtonClicked:(AMapNaviDriveView *)driveView
{//配置MoreMenu状态[self.moreMenu setTrackingMode:self.driveView.trackingMode];[self.moreMenu setShowNightType:self.driveView.showStandardNightType];[self.moreMenu setFrame:self.view.bounds];[self.view addSubview:self.moreMenu];
}- (void)driveViewTrunIndicatorViewTapped:(AMapNaviDriveView *)driveView
{NSLog(@"TrunIndicatorViewTapped");
}- (void)driveView:(AMapNaviDriveView *)driveView didChangeShowMode:(AMapNaviDriveViewShowMode)showMode
{NSLog(@"didChangeShowMode:%ld", (long)showMode);
}#pragma mark - MoreMenu Delegate- (void)moreMenuViewFinishButtonClicked
{[self.moreMenu removeFromSuperview];
}- (void)moreMenuViewNightTypeChangeTo:(BOOL)isShowNightType
{[self.driveView setShowStandardNightType:isShowNightType];
}- (void)moreMenuViewTrackingModeChangeTo:(AMapNaviViewTrackingMode)trackingMode
{[self.driveView setTrackingMode:trackingMode];
}
```
在导航控制器.m文件中引用的#import "SpeechSynthesizer.h" 为导航时的语音功能、
#import "MoreMenuView.h"为偏好设置界面![0383.png](http://upload-images.jianshu.io/upload_images/2536802-f44173668ba6ad44.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)SpeechSynthesizer.h文件内容:
```
#import <AVFoundation/AVFoundation.h>/***  iOS7及以上版本可以使用 AVSpeechSynthesizer 合成语音**  或者采用"科大讯飞"等第三方的语音合成服务*/
@interface SpeechSynthesizer : NSObject+ (instancetype)sharedSpeechSynthesizer;- (void)speakString:(NSString *)string;- (void)stopSpeak;
```
SpeechSynthesizer.m文件内容:
```
@interface SpeechSynthesizer () <AVSpeechSynthesizerDelegate>@property (nonatomic, strong, readwrite) AVSpeechSynthesizer *speechSynthesizer;@end@implementation SpeechSynthesizer+ (instancetype)sharedSpeechSynthesizer
{static id sharedInstance = nil;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{sharedInstance = [[SpeechSynthesizer alloc] init];});return sharedInstance;
}- (instancetype)init
{if (self = [super init]){[self buildSpeechSynthesizer];}return self;
}- (void)buildSpeechSynthesizer
{if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0){return;}//简单配置一个AVAudioSession以便可以在后台播放声音,更多细节参考AVFoundation官方文档AVAudioSession *session = [AVAudioSession sharedInstance];[session setCategory:AVAudioSessionCategoryPlayback error:NULL];self.speechSynthesizer = [[AVSpeechSynthesizer alloc] init];[self.speechSynthesizer setDelegate:self];
}- (void)speakString:(NSString *)string
{if (self.speechSynthesizer){AVSpeechUtterance *aUtterance = [AVSpeechUtterance speechUtteranceWithString:string];[aUtterance setVoice:[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"]];//iOS语音合成在iOS8及以下版本系统上语速异常if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0){aUtterance.rate = 0.25;//iOS7设置为0.25}else if ([[[UIDevice currentDevice] systemVersion] floatValue] < 9.0){aUtterance.rate = 0.15;//iOS8设置为0.15}if ([self.speechSynthesizer isSpeaking]){[self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryWord];}[self.speechSynthesizer speakUtterance:aUtterance];}
}- (void)stopSpeak
{if (self.speechSynthesizer){[self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];}
}

MoreMenuView.h文件内容:

#import <AMapNaviKit/AMapNaviCommonObj.h>@protocol MoreMenuViewDelegate;@interface MoreMenuView : UIView@property (nonatomic, assign) id<MoreMenuViewDelegate> delegate;@property (nonatomic, assign) AMapNaviViewTrackingMode trackingMode;
@property (nonatomic, assign) BOOL showNightType;@end@protocol MoreMenuViewDelegate <NSObject>
@optional- (void)moreMenuViewFinishButtonClicked;
- (void)moreMenuViewTrackingModeChangeTo:(AMapNaviViewTrackingMode)trackingMode;
- (void)moreMenuViewNightTypeChangeTo:(BOOL)isShowNightType;

MoreMenuView.m文件内容:

#define kFinishButtonHeight     40.0f
#define kOptionTableViewHieght  200.0f
#define kTableViewHeaderHeight  30.0f
#define kTableViewCellHeight    50.0f@interface MoreMenuView ()<UITableViewDataSource, UITableViewDelegate>
{UIView *_maskView;UITableView *_optionTableView;NSArray *_sections;NSArray *_options;UISegmentedControl *_viewModeSeg;UISegmentedControl *_nightTypeSeg;UIButton *_finishButton;
}@end@implementation MoreMenuView@synthesize trackingMode = _trackingMode;
@synthesize showNightType = _showNightType;#pragma mark - Initialization- (instancetype)initWithFrame:(CGRect)frame
{if (self = [super initWithFrame:frame]){self.backgroundColor = [UIColor clearColor];[self initProperties];[self createMoreMenuView];}return self;
}- (void)initProperties
{_sections = @[@"偏好设置"];_options = @[@[@"跟随模式", @"昼夜模式"]];
}- (void)createMoreMenuView
{[self initMaskView];[self initTableView];[self initFinishButton];
}- (void)initMaskView
{_maskView = [[UIView alloc] initWithFrame:self.bounds];_maskView.backgroundColor = [UIColor grayColor];_maskView.alpha = 0.5;_maskView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;[self addSubview:_maskView];
}- (void)initTableView
{_optionTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - kFinishButtonHeight - kOptionTableViewHieght, CGRectGetWidth(self.bounds), kOptionTableViewHieght)style:UITableViewStylePlain];_optionTableView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;_optionTableView.backgroundColor = [UIColor whiteColor];_optionTableView.delegate = self;_optionTableView.dataSource = self;_optionTableView.allowsSelection = NO;[self addSubview:_optionTableView];
}- (void)initFinishButton
{_finishButton = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - kFinishButtonHeight, CGRectGetWidth(self.bounds), kFinishButtonHeight)];_finishButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;[_finishButton setBackgroundColor:[UIColor blueColor]];[_finishButton setTitle:@"完 成" forState:UIControlStateNormal];[_finishButton addTarget:self action:@selector(finishButtonAction:) forControlEvents:UIControlEventTouchUpInside];[self addSubview:_finishButton];
}#pragma mark - TableView Delegate- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{return kTableViewHeaderHeight;
}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{return kTableViewCellHeight;
}#pragma mark - TableView DataSource Delegate- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{return _sections.count;
}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{return _sections[section];
}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{return [_options[section] count];
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{NSString *optionName = _options[indexPath.section][indexPath.row];if ([optionName isEqualToString:@"跟随模式"]){return [self tableViewCellForViewMode];}else if ([optionName isEqualToString:@"昼夜模式"]){return [self tableViewCellForNightType];}return nil;
}#pragma mark - Custom TableView Cell- (UITableViewCell *)tableViewCellForViewMode
{UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ViewModeCellIdentifier"];_viewModeSeg = [[UISegmentedControl alloc] initWithItems:@[@"正北朝上" , @"车头朝上"]];[_viewModeSeg setFrame:CGRectMake(160, (kTableViewCellHeight - 30)/2.0, 150, 30)];_viewModeSeg.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;[_viewModeSeg setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} forState:UIControlStateNormal];[_viewModeSeg addTarget:self action:@selector(viewModeSegmentedControlAction:) forControlEvents:UIControlEventValueChanged];UILabel *optionNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 120, kTableViewCellHeight)];optionNameLabel.textAlignment = NSTextAlignmentLeft;optionNameLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;optionNameLabel.font = [UIFont systemFontOfSize:18];optionNameLabel.text = @"跟随模式:";if (self.trackingMode == AMapNaviViewTrackingModeMapNorth){[_viewModeSeg setSelectedSegmentIndex:0];}else if (self.trackingMode == AMapNaviViewTrackingModeCarNorth){[_viewModeSeg setSelectedSegmentIndex:1];}[cell.contentView addSubview:optionNameLabel];[cell.contentView addSubview:_viewModeSeg];return cell;
}- (UITableViewCell *)tableViewCellForNightType
{UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ViewModeCellIdentifier"];UILabel *optionNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 120, kTableViewCellHeight)];optionNameLabel.textAlignment = NSTextAlignmentLeft;optionNameLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;optionNameLabel.font = [UIFont systemFontOfSize:18];optionNameLabel.text = @"昼夜模式:";_nightTypeSeg = [[UISegmentedControl alloc] initWithItems:@[@"白天" , @"黑夜"]];_nightTypeSeg.frame = CGRectMake(160, (kTableViewCellHeight - 30)/2.0, 150, 30);_nightTypeSeg.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;[_nightTypeSeg setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} forState:UIControlStateNormal];[_nightTypeSeg addTarget:self action:@selector(nightTypeSegmentedControlAction:) forControlEvents:UIControlEventValueChanged];[_nightTypeSeg setSelectedSegmentIndex:self.showNightType];[cell.contentView addSubview:optionNameLabel];[cell.contentView addSubview:_nightTypeSeg];return cell;
}#pragma mark - UISegmentedControl Action- (void)viewModeSegmentedControlAction:(id)sender
{NSInteger selectedIndex = [(UISegmentedControl *)sender selectedSegmentIndex];if (self.delegate && [self.delegate respondsToSelector:@selector(moreMenuViewTrackingModeChangeTo:)]){[self.delegate moreMenuViewTrackingModeChangeTo:(AMapNaviViewTrackingMode)selectedIndex];}
}- (void)nightTypeSegmentedControlAction:(id)sender
{NSInteger selectedIndex = [(UISegmentedControl *)sender selectedSegmentIndex];if (self.delegate && [self.delegate respondsToSelector:@selector(moreMenuViewNightTypeChangeTo:)]){[self.delegate moreMenuViewNightTypeChangeTo:selectedIndex];}
}#pragma mark - Finish Button Action- (void)finishButtonAction:(id)sender
{if (self.delegate && [self.delegate respondsToSelector:@selector(moreMenuViewFinishButtonClicked)]){[self.delegate moreMenuViewFinishButtonClicked];}
}

至此,导航功能已经完结。

如有不对的地方请私聊!!!

【iOS】高德地图MAMapKit的使用:导航功能。相关推荐

  1. flutter引入高德地图_Flutter笔记-调用原生IOS高德地图sdk

    一.前言 2017年底因公司业务组合部门调整,新的团队部分维护的项目用React Native技术混合开发.为适应环境变化,开启疯狂RN学习之旅,晚上回来啃资料看视频.可能由于本身对RN技术体验不感冒 ...

  2. 安卓高德地图开发之引入导航功能

    高德地图导航添加需要添加特定的navi包. 高德地图开发中添加导航功能的简单实现原理是引入自定义起点和终点数据后调用官方给出的demo代码. 首先需要一个baseactivity类,给出代码: pac ...

  3. 高德地图大头针功能_【iOS】高德地图MAMapKit的使用:地图显示、添加大头针、导航、定位功能介绍...

    最近在做基于高德地图的定位.导航及添加大头针的功能,特此记录下来...方便刚接触的同学参考... 一.申请 Key:获取用户Key 2.在"KEY管理"页面点击上方的"获 ...

  4. 【iOS】高德地图MAMapKit的使用:地图显示、添加大头针、导航、定位功能介绍

    最近在做基于高德地图的定位.导航及添加大头针的功能,特此记录下来...方便刚接触的同学参考... 一. 申请 Key 获取用户Key: 1.访问 http://lbs.amap.com/console ...

  5. [iOS]高德地图SDK开发--准备篇

    本文是对高德地图SDK使用的总结,对于高德地图不做过多介绍,可直接登录其官网开放平台进行了解; PS: 这里主要是讲解地图使用的准备工作,最后,以显示地图并定位到当前位置来验证;对于其他的使用,可参看 ...

  6. iOS 高德地图开发详解

    Demo地址 如果有所帮助记得关注,点Star demo中添加了查看路况功能,如果不需要,可以删除. ##一:基本地图功能实现 ####1.申请密钥流程 申请密钥链接 2.配置环境(重点) 高德地图提 ...

  7. iOS高德地图使用笔记

    高德地图笔记 一 准备工作 1.前往高德官网,申请key,http://lbs.amap.com/ 2.导入高德SDK,使用cocopods  platform :ios, '7.0' #手机的系统 ...

  8. iOS 高德地图(二)(进阶具体使用的细节)

    2019独角兽企业重金招聘Python工程师标准>>> 前面我们配置好了SDK的环境,也在高德的官网中申请了AppKey:de5b39fb2b066ed80c51383bb3a1fe ...

  9. iOS高德地图路径选择

    新公司的一个物流项目用到了高德地图的路径规划导航.之前没用过高德的路径规划,最麻烦的是画出路径,以及多路径情况下的点击选择路径. 其实画出路径的算法在高德地图的相关demo里面有,只要抠出来就行.我要 ...

  10. android-4集成高德地图的搜索和导航功能

    1.准备工作 1.1在高德地图导航包中用到的语音库为科大讯飞的,要使用的话也要在科大讯飞的开源平台上注册一个个人使用者key 1.2在使用高德地图的lib库时,最好使用导航和地图的合成库,如果导航库和 ...

最新文章

  1. python判断最后一行_python中如何判断文件的最后一行是空行?
  2. Java:判断一个字符串中是否存在另一个字符子串以及判断一个字符串中是否存在指定字符
  3. linux日志中有空格,linux中统计排序的内容含有空白行的解决办法
  4. UT斯达康XV6700的写号方法[图]
  5. Docker 部署 postgresql 与 pgadmin4
  6. 秒杀场景_解决秒杀超卖问题_04
  7. 30秒内限制函数只被调用一次
  8. gin c.Next()方法
  9. OC NSNumber NSValue
  10. 中国芯热情高涨 步履也更为坚实 近期我国多家企业再获“芯”突破
  11. 【免费看电视-直接看电视方法】打开电视发现看啥都要Vip?解决方法:
  12. android抓取微信朋友圈,一种快速提取Android微信朋友圈数据的方法
  13. 配电室智能监控系统设计及实现分析-Susie 周
  14. POI合并单元格,赋值
  15. 【PS】抠图,快速选择工具
  16. 互联网行业的众生相,不向命运低头的“英雄主义”
  17. 基于uniapp的个人课程表
  18. “更新 TKK 失败,请检查网络连接” 解决方法
  19. 深入理解Spring----PostConstruct和PreDestroy
  20. Python3.6+jieba+wordcloud 爬取豆瓣影评生成词云

热门文章

  1. 标记组的应用示例(重叠)_标靶图
  2. 保险双录政策再开放,菊风推出泛金融视频双录解决方案
  3. 第五课 竞争神经网络和SO(F)M神经网络——无监督神经网络模型
  4. FoneDog Toolkit iOS Data Recovery Mac(ios数据恢复)
  5. 连享会-空间计量专题 (2019.6.27-30)
  6. 用 Elasticsearch 造个“知网”难不难?
  7. 易基因课程回顾|表观遗传学和表观育种在品种改良中的应用研究
  8. 【AD封装】贴片及插件二极管-整流桥(带3D)
  9. java处理时区的注意事项
  10. 大龄程序员~聊聊我毕业十年的生活,kotlin入门书籍