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 )配置 下面是一 ...
随机推荐
- Codevs 1961 躲避大龙
1961 躲避大龙 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 你早上起来,慢悠悠地来到学校门口, ...
- [Xcode 实际操作]七、文件与数据-(16)解析XML文档
目录:[Swift]Xcode实际操作 本文将演示如何解析XML文档. 项目中已添加一份XML文档:worker.xml <?xml version="1.0" encodi ...
- [Xcode 实际操作]三、视图控制器-(2)UITabBarController选项卡(标签)视图控制器
目录:[Swift]Xcode实际操作 本文将为你演示,选项卡视图控制器的创建和使用. 在项目文件夹[DemoApp]上点击鼠标右键,弹出右键菜单. [New File]->[Cocoa Tou ...
- 学习Mahout (四)
在Mahout 学习(三)中,我贴了example的代码,里面生成向量文件的代码: InputDriver.runJob(input, directoryContainingConvertedInpu ...
- Spring源码(一)
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</lis ...
- solr IK分词器
1.把IK文件夹上传到服务器tmp文件夹 2.把需要的jar导入到solr项目中 # cp IKAnalyzer2012FF_u1.jar /usr/local/solr/tomcat/webapps ...
- 搭建Keepalived + Nginx + Tomcat的高可用负载均衡架构
1 概述 初期的互联网企业由于业务量较小,所以一般单机部署,实现单点访问即可满足业务的需求,这也是最简单的部署方式,但是随着业务的不断扩大,系统的访问量逐渐的上升,单机部署的模式已无法承载现有的业务量 ...
- Map集合的四种常用遍历方式整理
1.Map集合简介:map集合是一个key—value型的数据结构,存储的数据具有查询速度快速的特点,但由于是无序的,所以没有顺序可言.在遍历时没有办法像简单的list或数组一样. 2.代码: pac ...
- G-华华对月月的忠诚
链接:https://ac.nowcoder.com/acm/contest/392/G 题意: 月月要参加学校的信息学集训,晚上不能陪华华聊天了.不过为了防止华华去和别的小姐姐聊天,浪费时间影响学习 ...
- Codeforces Round #533(Div. 2) B.Zuhair and Strings
链接:https://codeforces.com/contest/1105/problem/B 题意: 给一个字符串和k,连续k个相同的字符,可使等级x加1, 例:8 2 aaacaabb 则有aa ...