百度地图api的实现

来源:互联网 发布:java扫描linux文件夹 编辑:程序博客网 时间:2024/05/29 10:40

iOS平台/开发指南

< iOS平台

目录

[隐藏]
  • 1 简介
    • 1.1 什么是百度地图API?
    • 1.2 获取API Key
    • 1.3 兼容性
  • 2 在您的程序中显示地图
    • 2.1 引入百度MapAPI的头文件
    • 2.2 引入静态库文件
    • 2.3 引入CoreLocation.framework和QuartzCore.framework
    • 2.4 引入mapapi.bundle资源文件
    • 2.5 初始化BMKMapManager
    • 2.6 创建BMKMapView
  • 3 注意事项
  • 4 卫星图图层
  • 5 实时路况图层
  • 6 地图覆盖物
    • 6.1 添加标注
    • 6.2 删除标注
    • 6.3 添加折线
    • 6.4 添加多边形
    • 6.5 添加圆
    • 6.6 删除Overlay
  • 7 服务类
    • 7.1 POI检索
    • 7.2 公交方案检索
    • 7.3 驾车路线检索
    • 7.4 步行路线检索
    • 7.5 地理编码
    • 7.6 反地理编码
    • 7.7 公交详情检索
  • 8 定位
  • 9 离线地图

简介

什么是百度地图API?

百度地图移动版API(IOS)是一套基于iOS3.0及以上设备的应用程序接口,通过该接口,您可以轻松访问百度服务和数据,构建功能丰富、交互性强的地图应用程序。百度地图移动版API不仅包含构建地图的基本接口,还提供了诸如地图定位、本地搜索、路线规划等数据服务,你可以根据自己的需要进行选择,目前支持Iphone3.0以上的版本,对iPad暂不支持。

面向的读者

API是提供给那些具有一定iOS编程经验和了解面向对象概念的读者使用。此外,读者还应该对地图产品有一定的了解。

您在使用中遇到任何问题,都可以通过API贴吧或交流群反馈给我们。

获取API Key

用户在使用API之前需要获取百度地图移动版API Key,APIKey可跨平台使用,如果您已经有Android平台的授权Key,可直接在iOS平台使用。该Key与你的百度账户相关联,您必须先有百度帐户,才能获得API KEY。并且,该KEY与您引用API的程序名称有关,具体流程请参照获取密钥。

兼容性

支持iOS3.0及以上系统,百度地图API接口与iOS内置的MapKit包兼容,开发者只需很小的改动即可完成从MapKit到百度地图API的迁移。并且迁移到百度地图API之后很多MapKit中只有iOS4.0以上版本才能使用的特性接口也可以正常使用了。

在您的程序中显示地图

完整的Demo例程可参考相关下载。

引入百度MapAPI的头文件

首先将百度MapAPI提供的头文件和静态库(.a)文件拷贝到您的工程目录下,在XCode中添加新的文件Group,引入百度MapAPI提供的头文件(请使用xcode 4.X以上平台)。

在您需要使用百度MapAPId的文件中添加以下代码

  1. #import "BMapKit.h"

引入静态库文件

百度MapAPI提供了模拟器和真机两中环境所使用的静态库文件,分别存放在libs/Release-iphonesimulator和libs/Release-iphoneos文件夹下。有两种方式可以引入静态库文件:

第一种方式:直接将对应平台的.a文件拖拽至XCode工程左侧的Groups&Files中,缺点是每次在真机和模拟器编译时都需要重新添加.a文件;

第二种方式:使用lipo命令将设备和模拟器的.a合并成一个通用的.a文件,将合并后的通用.a文件拖拽至工程中即可,具体命令如下:
lipo –create Release-iphoneos/libbaidumapapi.a Release-iphonesimulator/libbaidumapapi.a –output libbaidumapapi.a

第三种方式:

1.将API的libs文件夹拷贝到您的Application工程跟目录下

2.在XCode的Project -> Edit Active Target -> Build -> Linking -> Other Linker Flags中添加-lbaidumapapi

3.设置静态库的链接路径,在XCode的Project -> Edit Active Target -> Build -> Search Path -> Library Search Paths中添加您的静态库目录,比如"$(SRCROOT)/../libs/Release$(EFFECTIVE_PLATFORM_NAME)",$(SRCROOT)宏代表您的工程文件目录,$(EFFECTIVE_PLATFORM_NAME)宏代表当前配置是OS还是simulator

注:静态库中采用ObjectC++实现,因此需要您保证您工程中至少有一个.mm后缀的源文件(您可以将任意一个.m后缀的文件改名为.mm),或者在工程属性中指定编译方式,即将XCode的Project -> Edit Active Target -> Build -> GCC4.2 - Language -> Compile Sources As设置为"Objective-C++"

引入CoreLocation.framework和QuartzCore.framework

百度MapAPI中提供了定位功能和动画效果,因此您需要在您的XCode工程中引入CoreLocation.framework和QuartzCore.framework。 添加方式:右键点击Xcode工程左侧的Frameworks文件夹,add->Existing Frameworks,在弹出窗口中选中这两个framework,点击add即可。

引入mapapi.bundle资源文件

该步骤为可选,mapapi.bundle中存储了定位、默认大头针标注View及路线关键点的资源图片。如果您不需要使用内置的图片显示功能,则可以不添加此bundle文件。您也可以根据具体需求任意替换或删除该bundle中的图片文件。
添加方式:将mapapi.bundle拷贝到您的工程目录,直接将该bundle文件托拽至XCode工程左侧的Groups&Files中即可。

初始化BMKMapManager

在您的AppDelegate.h文件中添加BMKMapManager的定义

  1. @interface BaiduMapApiDemoAppDelegate : NSObject <UIApplicationDelegate> {
  2. UIWindow *window;
  3. UINavigationController *navigationController;
  4.  
  5. BMKMapManager* _mapManager;
  6. }

在您的AppDelegate.m文件中添加对BMKMapManager的初始化,并填入您申请的授权Key,示例如下

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  2. // 要使用百度地图,请先启动BaiduMapManager
  3. _mapManager = [[BMKMapManager alloc]init];
  4. // 如果要关注网络及授权验证事件,请设定generalDelegate参数
  5. BOOL ret = [_mapManager start:@"在此处输入您的授权Key" generalDelegate:nil];
  6. if (!ret) {
  7. NSLog(@"manager start failed!");
  8. }
  9. // Add the navigation controller's view to the window and display.
  10. [self.window addSubview:navigationController.view];
  11. [self.window makeKeyAndVisible];
  12. return YES;
  13. }

创建BMKMapView

在您的ViewController.m文件中添加BMKMapView的创建代码,示例如下

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. BMKMapView* mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
  4. self.view = mapView;
  5. }

编译,运行,效果如下图所示: map.png 
默认地图已经可以支持多点触摸,双击放大,多点单击缩小等操作,并都附带动画效果。

注意事项

1.静态库中采用ObjectC++实现,因此需要您保证您工程中至少有一个.mm后缀的源文件(您可以将任意一个.m后缀的文件改名为.mm),或者在工程属性中指定编译方式,即将XCode的Project -> Edit Active Target -> Build -> GCC4.2 - Language -> Compile Sources As设置为"Objective-C++"

2.如果您只在Xib文件中使用了BMKMapView,没有在代码中使用BMKMapView,编译器在链接时不会链接对应符合,需要在工程属性中显式设定:在XCode的Project -> Edit Active Target -> Build -> Linking -> Other Linker Flags中添加-all_load

3.授权Key的申请:授权Key可跨平台使用,如果您已经申请过Android的key,可直接在iOS中使用;如果还没有授权Key,请到http://dev.baidu.com/wiki/static/imap/key/ 页面申请

卫星图图层

  1. [mapView setMapType:BMKMapTypeSatellite];

运行后效果如下:satellite.png

实时路况图层

目前支持以下11个城市的实时路况信息:北京,上海,广州,深圳,南京,南昌,成都,重庆,武汉,大连,常州。在地图中通过以下代码设置显示实时路况图层:

打开实时路况图层:

  1. [mapView setMapType:BMKMapTypeTrafficOn];

关闭实时路况图层:

  1. [mapView setMapType:BMKMapTypeTrafficOff];

运行后效果如下:tra.png

地图覆盖物

覆盖物概述

地图上自定义的标注点和覆盖物我们统称为地图覆盖物。您可以通过定制BMKAnnotation和BMKOverlay来添加对应的标注点和覆盖物。地图覆盖物的设计遵循数据与View分离的原则,BMKAnnotation和BMKOverlay系列的类主要用来存放覆盖物相关的数据,BMKAnnotaionView和BMKOverlayView系列类为覆盖物对应的View。

添加标注

BMKAnnotation为标注对应的protocal,您可以自定义标注类实现该protocal。百度地图API也预置了基本的标注点:BMKPointAnnotation,和一个大头针标注View:BMKPinAnnotationView,您可以直接使用来显示标注。示例如下: 
修改您的ViewController.h文件,添加以下代码,使您的ViewController实现BMKMapViewDelegate协议:

  1. #import <UIKit/UIKit.h>
  2. #import "BMapKit.h"
  3. @interface AnnotationDemoViewController : UIViewController <BMKMapViewDelegate>{
  4. IBOutlet BMKMapView* mapView;
  5. }
  6. @end

修改您的ViewController.m文件,实现BMKMapViewDelegate的mapView:viewForAnnotation:函数,并在viewDidLoad添加标注数据对象

  1. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  2. - (void)viewDidLoad {
  3. [super viewDidLoad];
  4.  
  5. // 设置mapView的Delegate
  6. mapView.delegate = self;
  7.  
  8. // 添加一个PointAnnotation
  9. BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
  10. CLLocationCoordinate2D coor;
  11. coor.latitude = 39.915;
  12. coor.longitude = 116.404;
  13. annotation.coordinate = coor;
  14. annotation.title = @"这里是北京";
  15. [mapView addAnnotation:annotation];
  16. }
  17.  
  18. // Override
  19. - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
  20. {
  21. if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
  22. BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
  23. newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
  24. newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
  25. return newAnnotationView;
  26. }
  27. return nil;
  28. }

运行后,会在地图显示对应的标注点,点击会弹出气泡,效果如图:
annotation.png

删除标注

通过removeAnnotation:函数实现对已添加标注的删除功能,示例如下:

  1. if (annotation != nil) {
  2. [mapView removeAnnotation:annotation];
  3. }

添加折线

修改您的ViewController.h文件,添加以下代码,使您的ViewController实现BMKMapViewDelegate协议:

  1. #import <UIKit/UIKit.h>
  2. #import "BMapKit.h"
  3. @interface OverlayDemoViewController : UIViewController <BMKMapViewDelegate>{
  4. IBOutlet BMKMapView* mapView;
  5. }
  6. @end

修改您的ViewController.m文件,实现BMKMapViewDelegate的mapView:viewForOverlay:函数,并在viewDidLoad添加折线数据对象:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3.  
  4. // 设置delegate
  5. mapView.delegate = self;
  6.  
  7. // 添加折线覆盖物
  8. CLLocationCoordinate2D coors[2] = {0};
  9. coors[0].latitude = 39.315;
  10. coors[0].longitude = 116.304;
  11. coors[1].latitude = 39.515;
  12. coors[1].longitude = 116.504;
  13. BMKPolyline* polyline = [BMKPolyline polylineWithCoordinates:coors count:2];
  14. [mapView addOverlay:polyline];
  15. }
  16.  
  17. // Override
  18. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
  19. if ([overlay isKindOfClass:[BMKPolyline class]]){
  20. BMKPolylineView* polylineView = [[[BMKPolylineView alloc] initWithOverlay:overlay] autorelease];
  21. polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];
  22. polylineView.lineWidth = 5.0;
  23. return polylineView;
  24. }
  25. return nil;
  26. }

运行后,效果如图:polyline.png

添加多边形

修改您的ViewController.h文件,添加以下代码,使您的ViewController实现BMKMapViewDelegate协议: 
修改您的ViewController.m文件,实现BMKMapViewDelegate的mapView:viewForOverlay:函数,并在viewDidLoad添加多边形数据对象:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3.  
  4. // 设置delegate
  5. mapView.delegate = self;
  6.  
  7. // 添加多边形覆盖物
  8. CLLocationCoordinate2D coords[3] = {0};
  9. coords[0].latitude = 39;
  10. coords[0].longitude = 116;
  11. coords[1].latitude = 38;
  12. coords[1].longitude = 115;
  13. coords[2].latitude = 38;
  14. coords[2].longitude = 117;
  15. BMKPolygon* polygon = [BMKPolygon polygonWithCoordinates:coords count:3];
  16. [mapView addOverlay:polygon];
  17. }
  18.  
  19. // Override
  20. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
  21. if ([overlay isKindOfClass:[BMKPolygon class]]){
  22. BMKPolygonView* polygonView = [[[BMKPolygonView alloc] initWithOverlay:overlay] autorelease];
  23. polygonView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];
  24. polygonView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.2];
  25. polygonView.lineWidth = 5.0;
  26. return polygonView;
  27. }
  28. return nil;
  29. }

运行后,效果如图:polygon.png

添加圆

修改您的ViewController.h文件,添加以下代码,使您的ViewController实现BMKMapViewDelegate协议: 
修改您的ViewController.m文件,实现BMKMapViewDelegate的mapView:viewForOverlay:函数,并在viewDidLoad添加园数据对象:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3.  
  4. // 设置delegate
  5. mapView.delegate = self;
  6.  
  7. // 添加圆形覆盖物
  8. CLLocationCoordinate2D coor;
  9. coor.latitude = 39.915;
  10. coor.longitude = 116.404;
  11. BMKCircle* circle = [BMKCircle circleWithCenterCoordinate:coor radius:5000];
  12. [mapView addOverlay:circle];
  13. }
  14.  
  15. // Override
  16. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
  17. if ([overlay isKindOfClass:[BMKCircle class]]){
  18. BMKCircleView* circleView = [[[BMKCircleView alloc] initWithOverlay:overlay] autorelease];
  19. circleView.fillColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5];
  20. circleView.strokeColor = [[UIColor blueColor] colorWithAlphaComponent:0.5];
  21. circleView.lineWidth = 10.0;
  22. return circleView;
  23. }
  24. return nil;
  25. }

运行后,效果如图:circle.png

删除Overlay

通过removeOverlay:函数实现对已添加标注的删除功能,示例如下:

  1. if (overlay != nil) {
  2. [mapView removeOverlay:overlay];
  3. }

服务类

百度地图API提供的搜索服务包括:POI检索,多关键字检索,公交方案检索,驾车路线检索,步行路线检索,地理编码,反地理编码。 
所有检索请求接口均为异步接口,您必须实现BMKSearchDelegate协议,在检索到结果后,API会回调BMKSearchDelegate对应的接口,通知调用者检索结果数据。 
BMKSearchDelegate对应的接口如下:

  1. /**
  2.  *返回POI搜索结果
  3.  *@param poiResultList 搜索结果列表,成员类型为BMKPoiResult
  4.  *@param type 返回结果类型: BMKTypePoiList,BMKTypeAreaPoiList,BMKAreaMultiPoiList
  5.  *@param error 错误号,@see BMKErrorCode
  6.  */
  7. - (void)onGetPoiResult:(NSArray*)poiResultList searchType:(int)type errorCode:(int)error{
  8. }
  9. /**
  10.  *返回公交搜索结果
  11.  *@param result 搜索结果
  12.  *@param error 错误号,@see BMKErrorCode
  13.  */
  14. - (void)onGetTransitRouteResult:(BMKPlanResult*)result errorCode:(int)error{
  15. }
  16.  
  17. /**
  18.  *返回驾乘搜索结果
  19.  *@param result 搜索结果
  20.  *@param error 错误号,@see BMKErrorCode
  21.  */
  22. - (void)onGetDrivingRouteResult:(BMKPlanResult*)result errorCode:(int)error{
  23. }
  24. /**
  25.  *返回步行搜索结果
  26.  *@param result 搜索结果
  27.  *@param error 错误号,@see BMKErrorCode
  28.  */
  29. - (void)onGetWalkingRouteResult:(BMKPlanResult*)result errorCode:(int)error{
  30. }
  31. /**
  32.  *返回地址信息搜索结果
  33.  *@param result 搜索结果
  34.  *@param error 错误号,@see BMKErrorCode
  35.  */
  36. - (void)onGetAddrResult:(BMKAddrInfo*)result errorCode:(int)error{
  37. }
  38. /**
  39.  *返回公交详情搜索结果
  40.  *@param result 搜索结果
  41.  *@param error 错误号,@see BMKErrorCode
  42.  */
  43. - (void)onGetBusDetailResult:(BMKBusLineResult*)busLineResult errorCode:(int)error{
  44. }

POI检索

百度地图API提供以下几类POI检索类型:城市内检索,周边检索,范围检索,多关键字检索。 
此处以城市内检索为例说明: 
在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码如下:

  1. @interface PoiSearchDemoViewController : UIViewController<BMKMapViewDelegate, BMKSearchDelegate> {
  2. IBOutlet BMKMapView* _mapView;
  3. BMKSearch* _search;
  4. }

在ViewController.m的viewDidLoad中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取POI结果的方法,代码如下:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. _mapView.delegate = self;
  4. _search = [[BMKSearch alloc]init];
  5. _search.delegate = self;
  6. //发起POI检索
  7. [_search poiSearchInCity:@"北京" withKey:@"西单" pageIndex:0];
  8. }
  9. - (void)onGetPoiResult:(NSArray*)poiResultList searchType:(int)type errorCode:(int)error
  10. {
  11. if (error == BMKErrorOk) {
  12. BMKPoiResult* result = [poiResultList objectAtIndex:0];
  13. for (int i = 0; i < result.poiInfoList.count; i++) {
  14. BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
  15. BMKPointAnnotation* item = [[BMKPointAnnotation alloc]init];
  16. item.coordinate = poi.pt;
  17. item.title = poi.name;
  18. [_mapView addAnnotation:item];
  19. [item release];
  20. }
  21. }
  22. }

运行效果如图:poi.png 
示例代码请参考相关下载demo工程中的PoiSearchDemoViewController.mm文件

公交方案检索

在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码参考 POI检索中的示例代码.
在ViewController.m中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取公交路线结果的方法,代码如下:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. _mapView.delegate = self;
  4. _search = [[BMKSearch alloc]init];
  5. _search.delegate = self;
  6. //发起公交检索
  7. BMKPlanNode* start = [[BMKPlanNode alloc]init];
  8. start.name = @"天安门";
  9. BMKPlanNode* end = [[BMKPlanNode alloc]init];
  10. end.name = @"百度大厦";
  11. [_search transitSearch:@"北京" startNode:start endNode:end];
  12. [start release];
  13. [end release];
  14. }
  15. - (void)onGetTransitRouteResult:(BMKPlanResult*)result errorCode:(int)error
  16. {
  17. // 在此处添加您对公交方案结果的处理
  18. }

将公交方案对应的路线和关键点绘制在地图上,效果如下图:
transit.png 
示例代码请参考相关下载demo工程中的RouteSearchDemoViewController.mm文件

驾车路线检索

在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码参考 POI检索中的示例代码.
在ViewController.m中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取驾车路线结果的方法,代码如下:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. _mapView.delegate = self;
  4. _search = [[BMKSearch alloc]init];
  5. _search.delegate = self;
  6. //发起公交检索
  7. BMKPlanNode* start = [[BMKPlanNode alloc]init];
  8. start.name = @"天安门";
  9. BMKPlanNode* end = [[BMKPlanNode alloc]init];
  10. end.name = @"百度大厦";
  11. [_search drivingSearch:@"北京" startNode:start endCity:@"北京" endNode:end];
  12. [start release];
  13. [end release];
  14. }
  15. - (void)onGetDrivingRouteResult:(BMKPlanResult*)result errorCode:(int)error
  16. {
  17. // 在此处添加您对驾车方案结果的处理
  18. }

将驾车方案对应的路线和关键点绘制在地图上,效果如下图:
drive.png 
示例代码请参考相关下载demo工程中的RouteSearchDemoViewController.mm文件

步行路线检索

在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码参考 POI检索中的示例代码.
在ViewController.m中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取步行路线结果的方法,代码如下:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. _mapView.delegate = self;
  4. _search = [[BMKSearch alloc]init];
  5. _search.delegate = self;
  6. //发起步行检索
  7. BMKPlanNode* start = [[BMKPlanNode alloc]init];
  8. start.name = @"天安门";
  9. BMKPlanNode* end = [[BMKPlanNode alloc]init];
  10. end.name = @"百度大厦";
  11. [_search walkingSearch:@"北京" startNode:start endCity:@"北京" endNode:end];
  12. [start release];
  13. [end release];
  14. }
  15. - (void)onGetWalkingRouteResult:(BMKPlanResult*)result errorCode:(int)error
  16. {
  17. // 在此处添加您对步行方案结果的处理
  18. }

将步行方案对应的路线和关键点绘制在地图上,效果如下图:
walk.png 
示例代码请参考相关下载demo工程中的RouteSearchDemoViewController.mm文件

地理编码

在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码参考 POI检索中的示例代码.
在ViewController.m中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取地理编码结果的方法,代码如下:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. _mapView.delegate = self;
  4. _search = [[BMKSearch alloc]init];
  5. _search.delegate = self;
  6. //发起地理编码
  7. [_search geocode:@"东长安街33号" withCity:@"北京"];
  8. }
  9. - (void)onGetAddrResult:(BMKAddrInfo*)result errorCode:(int)error
  10. {
  11. // 在此处添加您对地理编码结果的处理
  12. }


完整的示例代码请参考相关下载demo工程中的GeocodeDemoViewController.mm文件

反地理编码

在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码参考 POI检索中的示例代码.
在ViewController.m中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取反地理编码结果的方法,代码如下:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. _mapView.delegate = self;
  4. _search = [[BMKSearch alloc]init];
  5. _search.delegate = self;
  6. //发起反地理编码
  7. CLLocationCoordinate2D pt = (CLLocationCoordinate2D){39.915101, 116.403981};
  8. [_search reverseGeocode:pt];
  9. }
  10. - (void)onGetAddrResult:(BMKAddrInfo*)result errorCode:(int)error
  11. {
  12. // 在此处添加您对反地理编码结果的处理
  13. }


完整的示例代码请参考相关下载demo工程中的GeocodeDemoViewController.mm文件

公交详情检索

在ViewController.h中声明BMKSearch对象,并将ViewController实现BMKSearchDelegate协议,代码参考 POI检索中的示例代码.
在ViewController.m中创建BMKSearch对象,设置对应的delegate,并实现BMKSearchDelegate协议中获取驾车路线结果的方法。发起公交详情搜索前,先进行POI检索,检索结果中poi.epoitype == 2时表示该类型为公交线路,才可发起公交搜索,代码如下:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. _mapView.delegate = self;
  4. _search = [[BMKSearch alloc]init];
  5. _search.delegate = self;
  6. //发起poi检索
  7. [_search poiSearchInCity:@"北京" withKey:@"717" pageIndex:0];
  8. }
  9. - (void)onGetPoiResult:(NSArray*)poiResultList searchType:(int)type errorCode:(int)error
  10. {
  11. if (error == BMKErrorOk) {
  12. BMKPoiResult* result = [poiResultList objectAtIndex:0];
  13. for (int i = 0; i < result.poiInfoList.count; i++) {
  14. BMKPoiInfo* poi = [result.poiInfoList objectAtIndex:i];
  15. if(poi.epoitype == 2)
  16. {
  17. break;
  18. }
  19. }
  20. // 发起公交详情搜索
  21. if(poi != nil && poi.epoitype == 2 )
  22. {
  23. NSLog(poi.uid);
  24. BOOL flag = [_search busLineSearch:@"北京" withKey:poi.uid];
  25. if (!flag) {
  26. NSLog(@"search failed!");
  27. }
  28. }
  29. }
  30. }
  31. - (void)onGetBusDetailResult:(BMKBusLineResult *)busLineResult errorCode:(int)error
  32. {
  33. // 在此处添加您对公交详情结果的处理
  34. }

将公交详情对应的路线和关键点绘制在地图上,效果如下图:
buslinedetail.png 
示例代码请参考相关下载demo工程中的BusLineSearchViewController.mm文件

定位

您可以通过以下代码来开启定位功能:

  1. [mapView setShowsUserLocation:YES];

定位成功后,可以通过mapView.userLocation来获取位置数据。 
完整的示例代码请参考相关下载demo工程中的LocationDemoViewController.mm文件

离线地图

SDK v1.1以后支持离线地图导入,从官网下载对应的离线包,通过itunes导入对应程序的共享目录,对于越狱的手机可以通过91助手拷到对应程序目录下的document目录:

  1. _offlineMap = [[BMKOfflineMap alloc]init];
  2. _offlineMap.delegate = self;


完整的示例代码请参考相关下载demo工程中的OfflineDemoViewController.m文件

原创粉丝点击