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 )配置 下面是一 ...
随机推荐
- numpy windows环境下载安装
由于numpy在多个平台下非常流行,以至于习惯WINDOWS环境下的用户可能找不到下载位置,更多的时候会下载到zip文件,然后需要安装编译(自然通不过) 1.http://www.scipy.org/ ...
- JQuery获取iframe中window对象的方法-contentWindow
document.getElementsByTagName('iframe')[0].contentWindow 获取到的就是iframe中的window对象.
- PAT甲级——1118 Birds in Forest (并查集)
此文章 同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/89819984 1118 Birds in Forest ...
- android studio ffmpeg简单使用 (cmake)
编译ffmpeg android studio 新建项目,勾选上 将编译好的libffmpeg.so库扔到src/main/jniLibs/armeabi下(主要这里我只编译了arm的ffmpeg的库 ...
- 运用session来控制用户的异地登录被挤下线情况
在用QQ的过程中我们如果你的账号在另外一台手机上面登录,这是腾讯后台会提醒你异地登录,可能你的账号被盗了,然后你手机上得QQ就会被退出登录,这个时候你就需要重新登录修改密码,以确保账号的安全.那这种被 ...
- Python type class metaclass
'type' 是 python built-in metaclass 其他继承自 ‘type’的class都可以是 Metaclass 子类可以继承父类的metaclass 然而 __metaclas ...
- CPU占用分析
用TOP命令很容易定位到时谁占用CPU最高 多线程的进程,我们要知道实际上占用cpu的最小单位是线程,所以肯定是众线程中的某一个或几个占用CPU过高导致的.top -H -p pid命令查看进程内各个 ...
- ecshop文章分类页面调用文章的内容
有的时候需要用到,所以总结了一下. 打开includes/lib_article.php文件 红色部分为添加的部分 function get_cat_articles($cat_id, $page = ...
- Asp.net开发必备51种代码
1.//弹出对话框.点击转向指定页面 Response.Write("<script>window.alert('该会员没有提交申请,请重新提交!')</script> ...
- Java虚拟机(JVM),JDK,JRE和JVM的区别——通过示例学习Java编程(2)
Java虚拟机(JVM),JDK,JRE和JVM的区别 作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid ...