baidumap demo(二)
接口说明
百度地图API提供的搜索服务包括:POI检索,多关键字检索,公交方案检索,驾车路线检索,步行路线检索,地理编码,反地理编码,公交详情检索,在线建议查询,短串分享。
所有检索请求接口均为异步接口,您必须实现BMKSearchDelegate协议,在一个时刻只能有一个BMKSearch接受回调消息,因此如果在不同的viewController中使用多个BMKSearch,需要在页面切换对BMKSearch的delegate做处理,代码如下:
- -(void)viewWillAppear:(BOOL)animated
- {
- _search.delegate = self;
- }
- -(void)viewWillDisappear:(BOOL)animated
- {
- _search.delegate = nil;
- }
在检索到结果后,API会回调BMKSearchDelegate对应的接口,通知调用者检索结果数据。
BMKSearchDelegate对应的接口如下:
- /**
- *返回POI搜索结果
- *@param poiResultList 搜索结果列表,成员类型为BMKPoiResult
- *@param type 返回结果类型: BMKTypePoiList,BMKTypeAreaPoiList,BMKAreaMultiPoiList
- *@param error 错误号,@see BMKErrorCode
- */
- - (void)onGetPoiResult:(NSArray*)poiResultList searchType:(int)type errorCode:(int)error{
- }
- /**
- *返回公交搜索结果
- *@param result 搜索结果
- *@param error 错误号,@see BMKErrorCode
- */
- - (void)onGetTransitRouteResult:(BMKPlanResult*)result errorCode:(int)error{
- }
- /**
- *返回驾乘搜索结果
- *@param result 搜索结果
- *@param error 错误号,@see BMKErrorCode
- */
- - (void)onGetDrivingRouteResult:(BMKPlanResult*)result errorCode:(int)error{
- }
- /**
- *返回步行搜索结果
- *@param result 搜索结果
- *@param error 错误号,@see BMKErrorCode
- */
- - (void)onGetWalkingRouteResult:(BMKPlanResult*)result errorCode:(int)error{
- }
- /**
- *返回地址信息搜索结果
- *@param result 搜索结果
- *@param error 错误号,@see BMKErrorCode
- */
- - (void)onGetAddrResult:(BMKAddrInfo*)result errorCode:(int)error{
- }
- /**
- *返回公交详情搜索结果
- *@param result 搜索结果
- *@param error 错误号,@see BMKErrorCode
- */
- - (void)onGetBusDetailResult:(BMKBusLineResult*)result errorCode:(int)error{
- }
- /**
- *返回suggestion搜索结果
- *@param result 搜索结果
- *@param error 错误号,@see BMKErrorCode
- */
- - (void)onGetSuggestionResult:(BMKSuggestionResult*)result errorCode:(int)error{
- }
- /**
- *返回poi详情分享url
- *@param url 返回结果url
- *@param error 错误号,@see BMKErrorCode
- */
- - (void)onGetShareUrl:(NSString*) url withType:(BMK_SHARE_URL_TYPE) urlType errorCode:(int)error {
- }
POI检索
百度地图API提供以下几类POI检索类型:城市内检索,周边检索,范围检索,多关键字检索。
此处以城市内检索为例说明:
在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码如下:
- @interface PoiSearchDemoViewController : UIViewController<BMKMapViewDelegate, BMKSearchDelegate> {
- IBOutlet BMKMapView* _mapView;
- BMKSearch* _search;
- }
在ViewController.m的viewDidLoad中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取POI结果的方法,代码如下:
- - (void)viewDidLoad {
- [super viewDidLoad];
- _search = [[BMKSearch alloc]init];
- _search.delegate = self;
- //发起POI检索
- [_search poiSearchInCity:@"北京" withKey:@"西单" pageIndex:0];
- }
- - (void)onGetPoiResult:(NSArray*)poiResultList searchType:(int)type errorCode:(int)error
- {
- if (error == BMKErrorOk) {
- BMKPoiResult* result = [poiResultList objectAtIndex:0];
- for (int i = 0; i < result.poiInfoList.count; i++) {
- BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
- BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
- item.coordinate = poi.pt;
- item.title = poi.name;
- [_mapView addAnnotation:item];
- [item release];
- }
- }
- }
运行效果如图:
示例代码请参考相关下载demo工程中的PoiSearchDemoViewController.mm文件
公交方案检索
在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码参考Demo中的示例代码.
在ViewController.m中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取公交路线结果的方法,代码如下:
- - (void)viewDidLoad {
- [super viewDidLoad];
- _search = [[BMKSearch alloc]init];
- _search.delegate = self;
- //发起公交检索
- BMKPlanNode* start = [[BMKPlanNode alloc]init];
- start.name = @"天安门";
- BMKPlanNode* end = [[BMKPlanNode alloc]init];
- end.name = @"百度大厦";
- [_search transitSearch:@"北京" startNode:start endNode:end];
- [start release];
- [end release];
- }
- - (void)onGetTransitRouteResult:(BMKPlanResult*)result errorCode:(int)error
- {
- // 在此处添加您对公交方案结果的处理
- }
将公交方案对应的路线和关键点绘制在地图上,效果如下图:
示例代码请参考相关下载demo工程中的RouteSearchDemoViewController.mm文件
驾车路线检索
在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码参考Demo中的示例代码.
在ViewController.m中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取驾车路线结果的方法,代码如下:
- - (void)viewDidLoad {
- [super viewDidLoad];
- _search = [[BMKSearch alloc]init];
- _search.delegate = self;
- //发起公交检索
- BMKPlanNode* start = [[BMKPlanNode alloc]init];
- start.name = @"天安门";
- BMKPlanNode* end = [[BMKPlanNode alloc]init];
- end.name = @"百度大厦";
- [_search drivingSearch:@"北京" startNode:start endCity:@"北京" endNode:end];
- [start release];
- [end release];
- }
- - (void)onGetDrivingRouteResult:(BMKPlanResult*)result errorCode:(int)error
- {
- // 在此处添加您对驾车方案结果的处理
- }
将驾车方案对应的路线和关键点绘制在地图上,效果如下图:
示例代码请参考相关下载demo工程中的RouteSearchDemoViewController.mm文件
驾车路线途经点检索
- BMKPlanNode* start = [[[BMKPlanNode alloc]init] autorelease];
- start.pt = startPt;
- start.name = _startAddrText.text;
- BMKPlanNode* end = [[[BMKPlanNode alloc]init] autorelease];
- end.name = _endAddrText.text;
- NSMutableArray * array = [[[NSMutableArray alloc] initWithCapacity:10] autorelease];
- BMKPlanNode* wayPoint1 = [[[BMKPlanNode alloc]init] autorelease];
- wayPoint1.cityName = @"北京";
- wayPoint1.name = @"清华大学";
- [array addObject:wayPoint1];
- BOOL flag = [_search drivingSearch:_startCityText.text startNode:start endCity:_endCityText.text endNode:end throughWayPoints:array];
注意:途经点传入的数组内容为BMKPlanNode,途经点长度最大限制为10个,超过10个途经点的请求不会被发送。BMKPlanNode数据结构新添加cityName属性,标记途经点的城市,如果不填默认用startCity。

步行路线检索
在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码参考Demo中的示例代码.
在ViewController.m中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取步行路线结果的方法,代码如下:
- - (void)viewDidLoad {
- [super viewDidLoad];
- _mapView.delegate = self;
- _search = [[BMKSearch alloc]init];
- _search.delegate = self;
- //发起步行检索
- BMKPlanNode* start = [[BMKPlanNode alloc]init];
- start.name = @"天安门";
- BMKPlanNode* end = [[BMKPlanNode alloc]init];
- end.name = @"百度大厦";
- [_search walkingSearch:@"北京" startNode:start endCity:@"北京" endNode:end];
- [start release];
- [end release];
- }
- - (void)onGetWalkingRouteResult:(BMKPlanResult*)result errorCode:(int)error
- {
- // 在此处添加您对步行方案结果的处理
- }
将步行方案对应的路线和关键点绘制在地图上,效果如下图:
示例代码请参考相关下载demo工程中的RouteSearchDemoViewController.mm文件
地理编码
在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码参考Demo中的示例代码.
在ViewController.m中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取地理编码结果的方法,代码如下:
- - (void)viewDidLoad {
- [super viewDidLoad];
- _mapView.delegate = self;
- _search = [[BMKSearch alloc]init];
- _search.delegate = self;
- //发起地理编码
- [_search geocode:@"东长安街33号" withCity:@"北京"];
- }
- - (void)onGetAddrResult:(BMKAddrInfo*)result errorCode:(int)error
- {
- // 在此处添加您对地理编码结果的处理
- }
完整的示例代码请参考相关下载demo工程中的GeocodeDemoViewController.mm文件
反地理编码
在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码参考Demo中的示例代码.
在ViewController.m中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取反地理编码结果的方法,代码如下:
- - (void)viewDidLoad {
- [super viewDidLoad];
- _search = [[BMKSearch alloc]init];
- _search.delegate = self;
- //发起反地理编码
- CLLocationCoordinate2D pt = (CLLocationCoordinate2D){0, 0};//此处填写想要反地理编码的坐标
- BOOL flag = [_search reverseGeocode:pt];
- }
- - (void)onGetAddrResult:(BMKAddrInfo*)result errorCode:(int)error
- {
- // 在此处添加您对反地理编码结果的处理
- }
完整的示例代码请参考相关下载demo工程中的GeocodeDemoViewController.mm文件
公交详情检索
在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码参考Demo中的示例代码.
在ViewController.m中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取驾车路线结果的方法。发起公交详情搜索前,先进行POI检索,检索结果中poi.epoitype == 2时表示该类型为公交线路,才可发起公交搜索,代码如下:
- - (void)viewDidLoad {
- [super viewDidLoad];
- _search = [[BMKSearch alloc]init];
- _search.delegate = self;
- //发起poi检索
- [_search poiSearchInCity:@"北京" withKey:@"717" pageIndex:0];
- }
- - (void)onGetPoiResult:(NSArray*)poiResultList searchType:(int)type errorCode:(int)error
- {
- if (error == BMKErrorOk) {
- BMKPoiResult* result = [poiResultList objectAtIndex:0];
- for (int i = 0; i < result.poiInfoList.count; i++) {
- BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
- if(poi.epoitype == 2)
- {
- break;
- }
- }
- // 发起公交详情搜索
- if(poi != nil && poi.epoitype == 2 )
- {
- NSLog(poi.uid);
- BOOL flag = [_search busLineSearch:@"北京" withKey:poi.uid];
- if (!flag) {
- NSLog(@"search failed!");
- }
- }
- }
- }
- - (void)onGetBusDetailResult:(BMKBusLineResult *)busLineResult errorCode:(int)error
- {
- // 在此处添加您对公交详情结果的处理
- }
将公交详情对应的路线和关键点绘制在地图上,效果如下图:
示例代码请参考相关下载demo工程中的BusLineSearchViewController.mm文件
在线建议查询
在线建议查询是指根据关键词查询在线建议词,代码如下:
在- (void)onGetSuggestionResult:(BMKSuggestionResult*)result errorCode:(int)error;接口中获取数据。
短串分享
短串分享是指,用户搜索查询后得到的每一个地理位置结果将会对应一条短串(短链接),用户可以通过短信、邮件或第三方分享组件(如微博、微信等)把短串分享给其他用户从而实现地理位置信息的分享。当其他用户收到分享的短串后,点击短串即可打开手机上的百度地图客户端或者手机浏览器进行查看。
例如,用户搜索"百度大厦"后通过短信使用短串分享功能把该地点分享给好友,好友点击短信中的短串"http://j.map.baidu.com/BkmBk"后可以调起百度地图客户端或者手机浏览器查看"百度大厦"的地理位置信息。
目前短串分享功能暂时开放了"POI搜索结果分享"和"反向地理编码结果分享",日后会开放更多的功能,欢迎广大开发者使用短串分享功能。
1、POI搜索结果分享
根据POI检索结果的UID,生成一个短连接,用于分享。方法为:- (BOOL)poiDetailShareUrl:(NSString*) uid;参数uid为待分享POI点的UID,代码如下:
2、反向地理编码结果分享
根据反向地理编码结果,生成一个用于分享的短连接。方法为:-(BOOL)reverseGeoShareUrl:(CLLocationCoordinate2D)coorpoiName:(NSString*)namepoiAddress:(NSString*)address;参数location:共享点经纬度坐标;name:共享点的名称;address:共享点的地址。代码如下:
- //发起短串搜索获取反geo分享url
- BOOL flag = [_search reverseGeoShareUrl:pt poiName:geoName poiAddress:addr];
3、获取短串分享结果
短串请求结果可以在BMKSearchDelegate的onGetShareUrl回调中获得,代码如下:
- //返回短串分享url
- - (void)onGetShareUrl:(NSString*) url withType:(BMK_SHARE_URL_TYPE) urlType errorCode:(int)error
- {
- shortUrl = url;
- if (error == BMKErrorOk)
- {
- if(showmeg!=nil)
- {
- [showmeg release];
- showmeg = nil;
- }
- showmeg = [[NSString stringWithFormat:@"这里是:%@\r\n%@\r\n%@",geoName,addr,short
- Url] retain];
- UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"短串分享" message
- :showmeg delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",@"取消",nil];
- myAlertView.tag = 1000;
- [myAlertView show];
- [myAlertView release];
- }
- }
调启百度导航
百度地图iOS SDK自v2.1.1版本起,为用户提供调启百度导航的能力。调启导航,分为两种:调启百度地图客户端导航和调启百度地图Web导航,用户只需很小的工作量,就可实现驾车路线导航的全套功能。
导航的核心代码如下,详细使用方法请参考Demo:
- //调启百度地图客户端导航
- - (IBAction)nativeNavi
- {
- //初始化调启导航时的参数管理类
- NaviPara* para = [[NaviPara alloc]init];
- //指定导航类型
- para.naviType = NAVI_TYPE_NATIVE;
- //初始化终点节点
- BMKPlanNode* end = [[[BMKPlanNode alloc]init] autorelease];
- //指定终点经纬度
- CLLocationCoordinate2D coor2;
- coor2.latitude = _nativeEndLatitude.text.floatValue;
- coor2.longitude = _nativeEndLongitude.text.floatValue;
- end.pt = coor2;
- //指定终点名称
- end.name = _nativeEndName.text;
- //指定终点
- para.endPoint = end;
- //指定返回自定义scheme,具体定义方法请参考常见问题
- para.appScheme = @"baidumapsdk://mapsdk.baidu.com";
- //调启百度地图客户端导航
- [BMKNavigation openBaiduMapNavigation:para];
- [para release];
- }
- //调启web导航
- - (IBAction)webNavi
- {
- //初始化调启导航时的参数管理类
- NaviPara* para = [[NaviPara alloc]init];
- //指定导航类型
- para.naviType = NAVI_TYPE_WEB;
- //初始化起点节点
- BMKPlanNode* start = [[[BMKPlanNode alloc]init] autorelease];
- //指定起点经纬度
- CLLocationCoordinate2D coor1;
- coor1.latitude = _webStartLatitude.text.floatValue;
- coor1.longitude = _webStartLongitude.text.floatValue;
- start.pt = coor1;
- //指定起点名称
- start.name = _webStartName.text;
- //指定起点
- para.startPoint = start;
- //初始化终点节点
- BMKPlanNode* end = [[[BMKPlanNode alloc]init] autorelease];
- CLLocationCoordinate2D coor2;
- coor2.latitude = _webEndLatitude.text.floatValue;
- coor2.longitude = _webEndLongitude.text.floatValue;;
- end.pt = coor2;
- para.endPoint = end;
- //指定终点名称
- end.name = _webEndName.text;
- //指定调启导航的app名称
- para.appName = [NSString stringWithFormat:@"%@", @"testAppName"];
- //调启web导航
- [BMKNavigation openBaiduMapNavigation:para];
- [para release];
- }
效果如下图:

baidumap demo(二)的更多相关文章
- 标签栏使用Demo二
// // PHTagViewFrame.m // 标签的使用二 // // Created by 123 on 16/9/6. // Copyright © 2016年 彭洪. All ri ...
- baidumap demo(三)
定位 您可以通过以下代码来开启定位功能: 源码复制打印关于 //开启定位功能 [_mapView setShowsUserLocation:YES]; 定位成功后,可以通过mapView.userLo ...
- Tornado 网站demo 二
连接数据库 methods 中建立一个文件 db.py 分别建立起连接对象和游标对象 #!/usr/bin/env Python # coding=utf-8 import pymysql conn ...
- baidumap demo(一)
覆盖物概述 地图上自定义的标注点和覆盖物我们统称为地图覆盖物.您可以通过定制BMKAnnotation和BMKOverlay来添加对应的标注点和覆盖物.地图覆盖物的设计遵循数据与View分离的原则,B ...
- ios 团购信息客户端demo(二)
接上一篇,这篇我们对我们的客户端加入KissXML,MBProgressHUD,AQridView这几个库,首先我们先加入KissXML,这是XML解析库,支持Xpath,可以方便添加更改任何节点.先 ...
- 使用Flexible适配移动端html页面 - demo记录
前段时间看了大神的博客文章[使用Flexible实现手淘H5页面的终端适配](地址:http://www.w3cplus.com/mobile/lib-flexible-for-html5-layou ...
- TensorFlow 在android上的Demo(1)
转载时请注明出处: 修雨轩陈 系统环境说明: ------------------------------------ 操作系统 : ubunt 14.03 _ x86_64 操作系统 内存: 8GB ...
- 第7篇 ORACLE EBS DEMO虚拟机环境的安装
ERP信息系统的实施不仅要求懂得道理方面的知识,更要侧重于应用实践.为了有一个稳定的测试环境.初学者可以自己搭建一个EBS DEMO环境.本节介绍EBS DEMO环境虚拟机的安装.一. 安装前的准备( ...
- echarts标准饼图(一)——基本配置demo
echarts标准饼图解读共分为四部分, 一.基本配置demo 二.标题(title)配置 三.提示框(tooltip)配置 四.图例(legend)配置 五.系列列表(series )配置 下面是一 ...
随机推荐
- Mol Cell Proteomics. |彭建祥| 人胃肠道间质瘤亚群蛋白质组图谱
大家好,本周分享的是发表在Molecular & Cellular Proteomics 上的一篇关于人胃肠道间质瘤亚群蛋白质组图谱的文章,题目是Proteomic maps of human ...
- 多次页面跳转后pop回主界面的问题
最近写代码的时候出了点BUG, 查阅资料后终于解决了. 问题原因大概是: 项目中所有的viewController都是继承自一个封装好的viewController. navigationbar, n ...
- 第二十篇 .NET高级技术之C#中的线程(二) 线程同步基础
1.同步要领 下面的表格列展了.NET对协调或同步线程动作的可用的工具: 简易阻止方法 构成 目的 Sleep 阻止给定的时间周期 Join 等待另一个线程 ...
- 001-JDK安装
1.确定JDK的具体版本号 [root@bogon ~]# rpm -qa | grep jdk java-1.7.0-openjdk-1.7.0.91-2.6.2.3.el7.x86_64 java ...
- [題解](單調隊列dp)luogu_P1725琪露諾
比較簡單的單調隊列,但是有一些要注意的 維護單調隊列的時候裡面存的是入隊時間,而不是i,因為前面有l個沒有入隊(不可能走進),所以把i减一个l以达到延迟入队的效果 #include<bits/s ...
- js-metisMenu
metisMenu是js的菜单插件,可以实现可折叠的二级菜单效果. 1 bootstrap折叠(Collapse) 直接引用bootstrap.js或者bootstrap.min.js就可以支持该插件 ...
- 使用JMeter上传文件
使用JMeter录制文件上载 创建JMeter测试计划的最简单方法是使用HTTP(s)测试脚本记录器记录相应的请求.JMeter充当代理服务器,捕获Web浏览器与被测应用程序(AUT)之间的所有流量, ...
- POJ-3159-Candies-(差分约束,Dijkstra)
链接:https://vjudge.net/problem/POJ-3159 题意: N个小孩,M个约束 以A,B,C给出.即小孩B的糖果不能比A多C以上. 思路: 差分约束: 若有 A-B < ...
- GYM 101673F(树计数)
树上每个割点计算一下各个size的组合相乘再相加为第一问答案,取最大的:再把本答案中最大的两个size相乘减掉,为第二问答案. const int maxn = 1e4 + 5; int n, siz ...
- css3のborder-radius
css3のborder-radius 今天主要练习了一下border-radius这个属性,这个是最常用的属性,所以先从它开始学习和总结吧. 我觉得需要注意以下几点: 1.书写规范: -webkit- ...