陈斌彬的技术博客

Stay foolish,stay hungry

iOS 百度导航 - Hello World(原创)

SDK组成

百度 iOS 导航 SDK 由四部分组成:

  • baiduNaviSDK.bundle 资源包
  • libbaiduNaviSDK.a 静态库
  • 头文件
  • BaiduStatistics

如图所示:

img

  • baiduNaviSDK.bundle 资源包由导航所需的配置数据、基础数据,以及导航功能所需的图片资源组成。
  • libbaiduNaviSDK.a 静态库,提供导航功能、线径规划、巡航等功能。
  • 头文件是libbaiduNaviSDK.a 静态库提供给第三方开发者的接口。
  • BaiduStatistics 是百度 mtj 统计库。

新建或打开工程

新建或打开一个 iOS 工程。新建时请选择支持 “iPhone"。

img

将 SDK 和 Framework 添加进工程

把 baiduNaviSDK 文件夹添加到工程中,把

  • AudioToolbox.framework
  • ImageIO.framework
  • CoreMotion.framework
  • CoreLocation.framework
  • CoreTelephony.framework
  • MediaPlayer.framework
  • AVFoundation.framework
  • SystemConfiguration.framework
  • libstdc++6.0.9.dylib

这几个 framework 添加到工程中,添加方法为在 Xcode 中单击工程文件,选择 Build Phrases 选项,点击 Link Binary with Libraries 下的 + 逐个添加,如下所示:

img

如果工程使用的是 ARC,在 Xcode 的 Build Phase 选项中设置 TouchJSON库设置为非 ARC 编译选项,如下:

img

修改Build Settings设置项

Build Settings 中, “Other Linker Flags” 添加 “-ObjC” 标识

img

结果如下:

img

将 “Build Active Architecture Only” 设置为 “No",将 "Valid Architecutres” 设置为 “armv7,arm64” ,删除 armv7s

img

配置.plist文件

设置 “required background modes” , “Supported interface orientations” , “NSLocationAlwaysUsageDescription”:

img

项目文件目录结构

img

开启引擎

AppDelegate.m 添加如下头文件

#import "BNCoreServices.h"

在函数 application:didFinishLaunchingWithOptions: 中启动 SDK 引擎,如下图所示。其中 KEY 值可以在 LBS 开放平台申请。

img

发起导航

发起导航前首先需要进行路径规划,如下:

//发起导航
- (void)startNavi
{
    //节点数组
    NSMutableArray *nodesArray = [[NSMutableArray alloc]    initWithCapacity:2];

    //起点
    BNRoutePlanNode *startNode = [[BNRoutePlanNode alloc] init];
    startNode.pos = [[BNPosition alloc] init];
    startNode.pos.x = 113.936392;
    startNode.pos.y = 22.547058;
startNode.pos.eType = BNCoordinate_BaiduMapSDK;
    [nodesArray addObject:startNode];

    //终点
    BNRoutePlanNode *endNode = [[BNRoutePlanNode alloc] init];
    endNode.pos = [[BNPosition alloc] init];
    endNode.pos.x = 114.077075;
    endNode.pos.y = 22.543634;
endNode.pos.eType = BNCoordinate_BaiduMapSDK;
    [nodesArray addObject:endNode];
    //发起路径规划
    [BNCoreServices_RoutePlan startNaviRoutePlan:BNRoutePlanMode_Recommend naviNodes:nodesArray time:nil delegete:self userInfo:nil];
}

img

算路成功后,在回调函数中发起导航,如下:

//算路成功回调
-(void)routePlanDidFinished:(NSDictionary *)userInfo
{
    NSLog(@"算路成功");

    //路径规划成功,开始导航
    [BNCoreServices_UI showNaviUI: BN_NaviTypeReal delegete:self isNeedLandscape:YES];
}

img

结果显示:

点击模拟导航

img

进入导航

img

调试输出

img

VimeContrller.h完整代码

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
      UIButton *loginButton;
}

@end

ViewController.m完整代码

#import "ViewController.h"
#import "BNRoutePlanModel.h"
#import "BNCoreServices.h"

@interface ViewController ()<BNNaviUIManagerDelegate,BNNaviRoutePlanDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    loginButton=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    loginButton.frame=CGRectMake(20, 60, 280, 30);
    [loginButton setTitle:@"模拟导航" forState:UIControlStateNormal];
    [loginButton setTintColor:[UIColor whiteColor]];
    [loginButton setBackgroundColor:[UIColor colorWithRed:192.0f/255.0f green:37.0f/255.0f blue:62.0f/255.0f alpha:1.0f]];
    [loginButton.layer setMasksToBounds:YES];
    [loginButton.layer setCornerRadius:10.0];
    [loginButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginButton];

}

-(void)buttonAction:(UIButton *)button{
    [self startNavi];
}

- (void)startNavi
{
    NSMutableArray *nodesArray = [[NSMutableArray alloc]initWithCapacity:2];
    //起点 传入的是原始的经纬度坐标,若使用的是百度地图坐标,可以使用BNTools类进行坐标转化
    BNRoutePlanNode *startNode = [[BNRoutePlanNode alloc] init];
    startNode.pos = [[BNPosition alloc] init];
    startNode.pos.x = 113.948222;
    startNode.pos.y = 22.549555;
    startNode.pos.eType = BNCoordinate_BaiduMapSDK;
    [nodesArray addObject:startNode];

    //也可以在此加入1到3个的途经点

    BNRoutePlanNode *midNode = [[BNRoutePlanNode alloc] init];
    midNode.pos = [[BNPosition alloc] init];
    midNode.pos.x = 113.977004;
    midNode.pos.y = 22.556393;
    midNode.pos.eType = BNCoordinate_BaiduMapSDK;
    [nodesArray addObject:midNode];

    //终点
    BNRoutePlanNode *endNode = [[BNRoutePlanNode alloc] init];
    endNode.pos = [[BNPosition alloc] init];
    endNode.pos.x = 114.089863;
    endNode.pos.y = 22.546236;
    endNode.pos.eType = BNCoordinate_BaiduMapSDK;
    [nodesArray addObject:endNode];

    [BNCoreServices_RoutePlan startNaviRoutePlan:BNRoutePlanMode_Highway naviNodes:nodesArray time:nil delegete:self userInfo:nil];
}


#pragma mark - BNNaviRoutePlanDelegate
//算路成功回调
-(void)routePlanDidFinished:(NSDictionary *)userInfo
{
    NSLog(@"算路成功");
    //路径规划成功,开始导航
    [BNCoreServices_UI showNaviUI:BN_NaviTypeSimulator delegete:self isNeedLandscape:YES];
}

AppDelegate.h完整代码

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m完整代码

#import "AppDelegate.h"
#import "BNCoreServices.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

       [self.window makeKeyAndVisible];

    //初始化导航SDK
    [BNCoreServices_Instance initServices:@"DSEGs6DvbZIB7PX8eWsDo3zO"];
    [BNCoreServices_Instance startServicesAsyn:nil fail:nil];

    return YES;
}

Resource Reference