前言:app中的导航功能越来越流行,现在我自己做的项目中也有此需求,做过了后记录下笔记。 由于源代码保密所以这里仅仅提供demo,下面是效果图
一:iOS地图SDK
1.打开 百度地图api链接
iOS地图SDK
2.注册成为开发者并按照步骤走
3.遇到的问题解决

4.遇到这种错误
2016-03-22 17:11:09.745 baiduMap[6664:276199] 地图所需资源文件不完整,请根据开发指南正确添加mapapi.bundle文件



成功:

4.注意BMKMapView 的类型
///地图View类,使用此View可以显示地图窗口,并且对地图进行相关的操作
@interface BMKMapView :UIView
5.集成基础地图

效果:

6.poi检索
遇到错误时候的处理方法点击错误码进去看看

可以用%zd把错误代码打印出来

- typedef enum{
- BMK_SEARCH_NO_ERROR = 0,
- BMK_SEARCH_AMBIGUOUS_KEYWORD,
- BMK_SEARCH_AMBIGUOUS_ROURE_ADDR,
- BMK_SEARCH_NOT_SUPPORT_BUS,
- BMK_SEARCH_NOT_SUPPORT_BUS_2CITY,
- BMK_SEARCH_RESULT_NOT_FOUND,
- BMK_SEARCH_ST_EN_TOO_NEAR,
- BMK_SEARCH_KEY_ERROR,
- BMK_SEARCH_NETWOKR_ERROR,
- BMK_SEARCH_NETWOKR_TIMEOUT,
- BMK_SEARCH_PERMISSION_UNFINISHED,
- }BMKSearchErrorCode;
检索成功代码:
- #import "ViewController.h"
- #import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
- #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
- #import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
- #import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
-
- @interface ViewController ()<BMKMapViewDelegate,BMKPoiSearchDelegate>
- @property (weak, nonatomic) IBOutlet BMKMapView *mapView;
- @property (nonatomic, strong) BMKPoiSearch *search;
- @end
-
- @implementation ViewController
- #pragma mark poi搜索
- - (BMKPoiSearch *)search
- {
- if (!_search) {
- _search = [[BMKPoiSearch alloc]init];
- _search.delegate = self;
- }
- return _search;
- }
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.mapView.delegate = self;
-
-
-
- BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
- option.pageIndex = 0;
- option.pageCapacity = 10;
- option.location = (CLLocationCoordinate2D){23.117055, 113.275995};
- option.keyword = @"小吃";
- BOOL flag = [self.search poiSearchNearBy:option];
- if(flag)
- {
- NSLog(@"周边检索发送成功");
- }
- else
- {
- NSLog(@"周边检索发送失败");
- }
-
- }
- #pragma mark BMKMapViewDelegate
- - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
- {
- if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
- BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
- newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
- newAnnotationView.animatesDrop = YES;
- return newAnnotationView;
- }
- return nil;
- }
-
- #pragma mark BMKPoiSearchDelegate
- - (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
- {
- if (error == BMK_SEARCH_NO_ERROR) {
-
-
- [poiResultList.poiInfoList enumerateObjectsUsingBlock:^(BMKPoiInfo * _Nonnull obj, NSUInteger idx, BOOLBOOL * _Nonnull stop) {
- NSLog(@"obj.pt = %@,obj.name = %@,obj.address = %@", obj.pt,obj.name,obj.address);
- }];
-
- }
- else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
-
-
- NSLog(@"起始点有歧义");
- } else {
- NSLog(@"抱歉,未找到结果--%zd", error);
- }
- }

将检索结果以大头针展示出来 代码
- #import "ViewController.h"
- #import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
- #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
- #import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
- #import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
-
- @interface ViewController ()<BMKMapViewDelegate,BMKPoiSearchDelegate>
- @property (weak, nonatomic) IBOutlet BMKMapView *mapView;
- @property (nonatomic, strong) BMKPoiSearch *search;
- @end
-
- @implementation ViewController
- #pragma mark poi搜索
- - (BMKPoiSearch *)search
- {
- if (!_search) {
- _search = [[BMKPoiSearch alloc]init];
- _search.delegate = self;
- }
- return _search;
- }
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.mapView.delegate = self;
-
-
-
-
- }
- -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
- {
-
- BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
- option.pageIndex = 0;
- option.pageCapacity = 10;
- option.location = (CLLocationCoordinate2D){39.915, 116.404};
-
- option.keyword = @"大学";
- BOOL flag = [self.search poiSearchNearBy:option];
- if(flag)
- {
- NSLog(@"周边检索发送成功");
- }
- else
- {
- NSLog(@"周边检索发送失败");
- }
- }
-
-
- #pragma mark BMKMapViewDelegate
- - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
- {
- if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
- BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
- newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
- newAnnotationView.animatesDrop = YES;
- return newAnnotationView;
- }
- return nil;
- }
-
- #pragma mark BMKPoiSearchDelegate 处理POI检索结果
- - (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
- {
- if (error == BMK_SEARCH_NO_ERROR) {
-
-
- [poiResultList.poiInfoList enumerateObjectsUsingBlock:^(BMKPoiInfo * _Nonnull obj, NSUInteger idx, BOOLBOOL * _Nonnull stop) {
- NSLog(@"obj.pt = %@,obj.name = %@,obj.address = %@", obj.pt,obj.name,obj.address);
- [self addAnnoWithPT:obj.pt andTitle:obj.name andAddress:obj.address];
- }];
-
- }
- else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
-
-
- NSLog(@"起始点有歧义");
- } else {
- NSLog(@"抱歉,未找到结果--%zd", error);
- }
- }
- #pragma mark 检索成功添加大头针
- - (void)addAnnoWithPT:(CLLocationCoordinate2D)coor andTitle:(NSString *)title andAddress:(NSString *)address
- {
- BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
- annotation.coordinate = coor;
- annotation.title = title;
- annotation.subtitle = address;
- [self.mapView addAnnotation:annotation];
- }
-
- @end
结果:

用这个方法将长按的地方作为要检索的区域
-(void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate
代码:
- #import "ViewController.h"
- #import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
- #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
- #import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
- #import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
-
- @interface ViewController ()<BMKMapViewDelegate,BMKPoiSearchDelegate>
- @property (weak, nonatomic) IBOutlet BMKMapView *mapView;
- @property (nonatomic, strong) BMKPoiSearch *search;
- @end
-
- @implementation ViewController
- #pragma mark poi搜索
- - (BMKPoiSearch *)search
- {
- if (!_search) {
- _search = [[BMKPoiSearch alloc]init];
- _search.delegate = self;
- }
- return _search;
- }
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.mapView.delegate = self;
-
-
-
-
- }
- -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
- {
-
-
-
- [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(23.132931, 113.375924)];
- }
- #pragma mark 在此方法中打印定位的经纬度跨度
- -(void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
- {
- NSLog(@"%f---%f", mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);
- }
-
- #pragma mark BMKMapViewDelegate
- - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
- {
- if ([annotation isKindOfClass:[BMKPointAnnotation class]]){
- BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
- newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
- newAnnotationView.animatesDrop = YES;
- return newAnnotationView;
- }
- return nil;
- }
-
- #pragma mark BMKPoiSearchDelegate 处理POI检索结果
- - (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
- {
- if (error == BMK_SEARCH_NO_ERROR) {
-
-
- [poiResultList.poiInfoList enumerateObjectsUsingBlock:^(BMKPoiInfo * _Nonnull obj, NSUInteger idx, BOOLBOOL * _Nonnull stop) {
- NSLog(@"obj.pt = %@,obj.name = %@,obj.address = %@", obj.pt,obj.name,obj.address);
- [self addAnnoWithPT:obj.pt andTitle:obj.name andAddress:obj.address];
- }];
-
- }
- else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
-
-
- NSLog(@"起始点有歧义");
- } else {
- NSLog(@"抱歉,未找到结果--%zd", error);
- }
- }
- #pragma mark 检索成功添加大头针
- - (void)addAnnoWithPT:(CLLocationCoordinate2D)coor andTitle:(NSString *)title andAddress:(NSString *)address
- {
- BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
- annotation.coordinate = coor;
- annotation.title = title;
- annotation.subtitle = address;
- [self.mapView addAnnotation:annotation];
- }
- #pragma mark 将长按的地方作为要检索的区域
- -(void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate
- {
- BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
- option.pageIndex = 0;
- option.pageCapacity = 30;
- option.location = coordinate;
- option.keyword = @"大学";
- BOOL flag = [self.search poiSearchNearBy:option];
- if(flag)
- {
- NSLog(@"周边检索发送成功");
- }
- else
- {
- NSLog(@"周边检索发送失败");
- }
-
- [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)];
- }
- @end
效果:

二:iOS导航SDK
1.打开链接
iOS导航SDK
2.按照上面给出的步骤配置好

效果:


代码:
-
- #import "ViewController.h"
- #import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
- #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
- #import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
- #import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
- #import "BNCoreServices.h"
- @interface ViewController ()<BMKMapViewDelegate,BMKPoiSearchDelegate,BNNaviRoutePlanDelegate>
- @property (weak, nonatomic) IBOutlet BMKMapView *mapView;
- @property (nonatomic, strong) BMKPoiSearch *search;
- @end
-
- @implementation ViewController
- #pragma mark poi搜索
- - (BMKPoiSearch *)search
- {
- if (!_search) {
- _search = [[BMKPoiSearch alloc]init];
- _search.delegate = self;
- }
- return _search;
- }
-
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.mapView.delegate = self;
-
-
-
-
- }
-
- #pragma mark 发起导航 路线规划
- - (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];
- }
- -(void)routePlanDidFinished:(NSDictionary *)userInfo
- {
- NSLog(@"算路成功");
-
-
- [BNCoreServices_UI showNaviUI: BN_NaviTypeReal delegete:nil isNeedLandscape:YES];
-
- }
-
- -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
- {
- [self startNavi];
- [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(23.132931, 113.375924)];
- }
- #pragma mark 2.在此方法中打印定位的经纬度跨度
- -(void)mapView:(BMKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
- {
- NSLog(@"%f---%f", mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);
- }
- #pragma mark 3:根据anntation生成对应的View
- - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
- {
- if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
- static NSString *pinID = @"PIN";
- BMKPinAnnotationView *newAnnotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pinID];
- if (newAnnotationView == nil) {
- newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinID];
- }
- newAnnotationView.annotation = annotation;
-
- newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
- newAnnotationView.animatesDrop = YES;
-
-
- UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
- [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
- newAnnotationView.rightCalloutAccessoryView = btn;
-
- return newAnnotationView;
- }
- return nil;
- }
-
- - (void)click
- {
- NSLog(@"开始导航");
-
- }
-
- #pragma mark BMKPoiSearchDelegate 4.处理POI检索结果
- - (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
- {
- if (error == BMK_SEARCH_NO_ERROR) {
-
-
- [poiResultList.poiInfoList enumerateObjectsUsingBlock:^(BMKPoiInfo * _Nonnull obj, NSUInteger idx, BOOLBOOL * _Nonnull stop) {
- NSLog(@"obj.pt = %@,obj.name = %@,obj.address = %@", obj.pt,obj.name,obj.address);
- [self addAnnoWithPT:obj.pt andTitle:obj.name andAddress:obj.address];
- }];
-
- }
- else if (error == BMK_SEARCH_AMBIGUOUS_KEYWORD){
-
-
- NSLog(@"起始点有歧义");
- } else {
- NSLog(@"抱歉,未找到结果--%zd", error);
- }
- }
- #pragma mark 检索成功添加大头针
- - (void)addAnnoWithPT:(CLLocationCoordinate2D)coor andTitle:(NSString *)title andAddress:(NSString *)address
- {
- BMKPointAnnotation * annotation = [[BMKPointAnnotation alloc]init];
- annotation.coordinate = coor;
- annotation.title = title;
- annotation.subtitle = address;
- [self.mapView addAnnotation:annotation];
- }
- #pragma mark 1. 将长按的地方作为要检索的区域
- -(void)mapview:(BMKMapView *)mapView onLongClick:(CLLocationCoordinate2D)coordinate
- {
- BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
- option.pageIndex = 0;
- option.pageCapacity = 30;
- option.location = coordinate;
- option.keyword = @"大学";
- BOOL flag = [self.search poiSearchNearBy:option];
- if(flag)
- {
- NSLog(@"周边检索发送成功");
- }
- else
- {
- NSLog(@"周边检索发送失败");
- }
- [self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)];
- }
- @end
模拟导航效果:

语音播报:
iOS集成百度地图方法步骤
原文:http://www.cnblogs.com/wanghuaijun/p/5885332.html