接口说明
百度地图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对应的接口如下:
-
-
-
-
-
-
- - (void)onGetPoiResult:(NSArray*)poiResultList searchType:(int)type errorCode:(int)error{
- }
-
-
-
-
-
- - (void)onGetTransitRouteResult:(BMKPlanResult*)result errorCode:(int)error{
- }
-
-
-
-
-
-
- - (void)onGetDrivingRouteResult:(BMKPlanResult*)result errorCode:(int)error{
- }
-
-
-
-
-
- - (void)onGetWalkingRouteResult:(BMKPlanResult*)result errorCode:(int)error{
- }
-
-
-
-
-
- - (void)onGetAddrResult:(BMKAddrInfo*)result errorCode:(int)error{
- }
-
-
-
-
-
- - (void)onGetBusDetailResult:(BMKBusLineResult*)result errorCode:(int)error{
- }
-
-
-
-
-
- - (void)onGetSuggestionResult:(BMKSuggestionResult*)result errorCode:(int)error{
- }
-
-
-
-
-
- - (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;
-
- [_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;
-
- [_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文件
在线建议查询
在线建议查询是指根据关键词查询在线建议词,代码如下:
- [_search suggestionSearch:@"西单" inCity:@"北京"];
在-
(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,代码如下:
-
- BOOL flag = [_search poiDetailShareUrl:poi.uid];
2、反向地理编码结果分享
根据反向地理编码结果,生成一个用于分享的短连接。方法为:-(BOOL)reverseGeoShareUrl:(CLLocationCoordinate2D)coorpoiName:(NSString*)namepoiAddress:(NSString*)address;参数location:共享点经纬度坐标;name:共享点的名称;address:共享点的地址。代码如下:
-
- BOOL flag = [_search reverseGeoShareUrl:pt poiName:geoName poiAddress:addr];
3、获取短串分享结果
短串请求结果可以在BMKSearchDelegate的onGetShareUrl回调中获得,代码如下:
-
- - (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;
-
-
- para.appScheme = @"baidumapsdk://mapsdk.baidu.com";
-
- [BMKNavigation openBaiduMapNavigation:para];
- [para release];
- }
-
- - (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;
-
- para.appName = [NSString stringWithFormat:@"%@", @"testAppName"];
-
- [BMKNavigation openBaiduMapNavigation:para];
- [para release];
- }
-
效果如下图:

baidumap demo(二)
原文:http://www.cnblogs.com/yulang314/p/3549748.html