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,///
检索成功代码:
#import "ViewController.h"
#import //引入base相关所有的头文件
#import //引入地图功能所有的头文件
#import //引入检索功能所有的头文件
#import //引入定位功能所有的头文件
@interface ViewController ()
@property (weak, nonatomic) IBOutlet BMKMapView *mapView;
//@property(strong,nonatomic)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];
// BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
// self.view = mapView;
self.mapView.delegate = self;
// 添加一个PointAnnotation
// BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
// CLLocationCoordinate2D coor;
// coor.latitude = 39.915;
// coor.longitude = 116.404;
// annotation.coordinate = coor;
// annotation.title = @"这里是China";
// annotation.subtitle = @"这是中国";
// [_mapView addAnnotation:annotation];
//发起检索
BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
option.pageIndex = 0;
option.pageCapacity = 10;
// option.location = (CLLocationCoordinate2D){39.915, 116.404};//北京
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 )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) {
//在此处理正常结果
// NSLog(@"%@", poiResultList.poiInfoList);
[poiResultList.poiInfoList enumerateObjectsUsingBlock:^(BMKPoiInfo * _Nonnull obj, NSUInteger idx, BOOL * _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){
//当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
// result.cityList;
NSLog(@"起始点有歧义");
} else {
NSLog(@"抱歉,未找到结果--%zd", error);
}
}
@end
将检索结果以大头针展示出来 代码
#import "ViewController.h"
#import //引入base相关所有的头文件
#import //引入地图功能所有的头文件
#import //引入检索功能所有的头文件
#import //引入定位功能所有的头文件
@interface ViewController ()
@property (weak, nonatomic) IBOutlet BMKMapView *mapView;
//@property(strong,nonatomic)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];
// BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
// self.view = mapView;
self.mapView.delegate = self;
// 添加一个PointAnnotation
// BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
// CLLocationCoordinate2D coor;
// coor.latitude = 39.915;
// coor.longitude = 116.404;
// annotation.coordinate = coor;
// annotation.title = @"这里是China";
// annotation.subtitle = @"这是中国";
// [_mapView addAnnotation:annotation];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//发起检索
BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
option.pageIndex = 0;
option.pageCapacity = 10;
option.location = (CLLocationCoordinate2D){39.915, 116.404};//北京
// 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 )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) {
//在此处理正常结果
// NSLog(@"%@", poiResultList.poiInfoList);
[poiResultList.poiInfoList enumerateObjectsUsingBlock:^(BMKPoiInfo * _Nonnull obj, NSUInteger idx, BOOL * _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){
//当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
// result.cityList;
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 //引入base相关所有的头文件
#import //引入地图功能所有的头文件
#import //引入检索功能所有的头文件
#import //引入定位功能所有的头文件
@interface ViewController ()
@property (weak, nonatomic) IBOutlet BMKMapView *mapView;
//@property(strong,nonatomic)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];
// BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
// self.view = mapView;
self.mapView.delegate = self;
// 添加一个PointAnnotation
// BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
// CLLocationCoordinate2D coor;
// coor.latitude = 39.915;
// coor.longitude = 116.404;
// annotation.coordinate = coor;
// annotation.title = @"这里是China";
// annotation.subtitle = @"这是中国";
// [_mapView addAnnotation:annotation];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//发起检索
// BMKNearbySearchOption *option = [[BMKNearbySearchOption alloc]init];
// option.pageIndex = 0;
// option.pageCapacity = 10;
//// option.location = (CLLocationCoordinate2D){39.915, 116.404};//北京
//
//
// option.location = (CLLocationCoordinate2D){23.117055, 113.275995};//广州
//// option.keyword = @"大学"; //北京检索大学
// option.keyword = @"小吃"; //广州检索小吃
// BOOL flag = [self.search poiSearchNearBy:option];
// if(flag)
// {
// NSLog(@"周边检索发送成功");
// }
// else
// {
// NSLog(@"周边检索发送失败");
// }
[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 )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) {
//在此处理正常结果
// NSLog(@"%@", poiResultList.poiInfoList);
[poiResultList.poiInfoList enumerateObjectsUsingBlock:^(BMKPoiInfo * _Nonnull obj, NSUInteger idx, BOOL * _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){
//当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
// result.cityList;
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(@"周边检索发送失败");
}
// CLLocationCoordinate2D center = option.location;
// BMKCoordinateSpan span = BMKCoordinateSpanMake(0.021686, 0.014705);
// BMKCoordinateRegion region = BMKCoordinateRegionMake(center, span);
// [self.mapView setRegion:region animated:YES];
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)]; //将地图自动定位到检索到的区域
}
@end
效果:
1.打开链接
iOS导航SDK
2.按照上面给出的步骤配置好
效果:
代码:
//
// ViewController.m
// baiduMap
//
// Created by Vitco on 16/3/22.
// Copyright © 2016年 Vitco. All rights reserved.
//
#import "ViewController.h"
#import //引入base相关所有的头文件
#import //引入地图功能所有的头文件
#import //引入检索功能所有的头文件
#import //引入定位功能所有的头文件
#import "BNCoreServices.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet BMKMapView *mapView;
//@property(strong,nonatomic)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];
// BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
// self.view = mapView;
self.mapView.delegate = self;
// 添加一个PointAnnotation
// BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
// CLLocationCoordinate2D coor;
// coor.latitude = 39.915;
// coor.longitude = 116.404;
// annotation.coordinate = coor;
// annotation.title = @"这里是China";
// annotation.subtitle = @"这是中国";
// [_mapView addAnnotation:annotation];
}
#pragma mark 发起导航 路线规划
//- (void)startNaviWithEndPT:(CLLocationCoordinate2D)endPT
//{
// //节点数组
// NSMutableArray *nodesArray = [[NSMutableArray alloc] initWithCapacity:2];
//
// //起点
// BNRoutePlanNode *startNode = [[BNRoutePlanNode alloc] init];
// startNode.pos = [[BNPosition alloc] init];
// startNode.pos.x = 113.375924;
// startNode.pos.y = 23.132931;
// startNode.pos.eType = BNCoordinate_BaiduMapSDK;
// [nodesArray addObject:startNode];
//
// //终点
// BNRoutePlanNode *endNode = [[BNRoutePlanNode alloc] init];
// endNode.pos = [[BNPosition alloc] init];
// endNode.pos.x = endPT.longitude;
// endNode.pos.y = endPT.latitude;
// endNode.pos.eType = BNCoordinate_BaiduMapSDK;
// [nodesArray addObject:endNode];
// //发起路径规划
// [BNCoreServices_RoutePlan startNaviRoutePlan:BNRoutePlanMode_Recommend naviNodes:nodesArray time:nil delegete:self userInfo:nil];
//}
//发起导航
- (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:self isNeedLandscape:YES];
[BNCoreServices_UI showNaviUI: BN_NaviTypeReal delegete:nil isNeedLandscape:YES];
}
-(void)touchesBegan:(NSSet *)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
/**
*根据anntation生成对应的View
*@param mapView 地图View
*@param annotation 指定的标注
*@return 生成的标注View
*/
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id )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(@"开始导航");
// CLLocationCoordinate2D coordinate = _selectAnno.coordinate;
// [self startNaviWithEndPT:coordinate];
}
#pragma mark BMKPoiSearchDelegate 4.处理POI检索结果
- (void)onGetPoiResult:(BMKPoiSearch*)searcher result:(BMKPoiResult*)poiResultList errorCode:(BMKSearchErrorCode)error
{
if (error == BMK_SEARCH_NO_ERROR) {
//在此处理正常结果
// NSLog(@"%@", poiResultList.poiInfoList);
[poiResultList.poiInfoList enumerateObjectsUsingBlock:^(BMKPoiInfo * _Nonnull obj, NSUInteger idx, BOOL * _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){
//当在设置城市未找到结果,但在其他城市找到结果时,回调建议检索城市列表
// result.cityList;
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(@"周边检索发送失败");
}
// CLLocationCoordinate2D center = option.location;
// BMKCoordinateSpan span = BMKCoordinateSpanMake(0.021686, 0.014705);
// BMKCoordinateRegion region = BMKCoordinateRegionMake(center, span);
// [self.mapView setRegion:region animated:YES];
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(coordinate.latitude,coordinate.longitude)]; //将地图自动定位到检索到的区域
}
@end
模拟导航效果:
语音播报: