继上一篇博客之后的导航功能。
线路规划主要代码:
//进行路径规划
[self.driveManager calculateDriveRouteWithStartPoints:@[self.startPoint]
endPoints:@[self.endPoint]
wayPoints:nil
drivingStrategy: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];