分享

iOS中百度地图API的总结

 求知665 2014-11-01

这篇文章记录了:

引入百度地图API

如何显示地图并定位

如何定位获取经纬度

如何通过定位得到城市,国家,街道等信息

如何通过搜索地理名获得坐标

如何实现公交和驾车路线搜索

如何实现当前位置到指定位置的公交和驾车路线搜索

引入百度地图API

首先,需要到http://dev.baidu.com/wiki/imap/index.php?title=iOS平台/相关下载下载全部内容,包括文档,示例代码和开发包。

然后获取自己的API KEY,具体方法按百度的官网申请就行,比较简单。

下载的文件应该有三个

把inc文件夹拖入到项目中去,引入了头文件,然后如果用真机就把Release-iphoneos里面的.a文件拖拽到项目中去,最后别忘了拖入mapapi.bundle文件,路线节点和图钉的图片来源于素材包。

此外还要引入CoreLocation.framework和QuartzCore.framework,这样引入工作就大功告成,但是要注意一点很重要的,静态库中采用ObjectC++实现,因此需要保证工程中至少有一个.mm后缀的源文件(您可以将任意一个.m后缀的文件改名为.mm),或者在工程属性中指定编译方式,即将XCode的Project-> Edit Active Target -> Build -> GCC4.2 – Language ->Compile Sources As设置为”Objective-C++”。

经过实践,我推荐不这么干,默认是根据文件类型来选择编译的方式,文件要是.m就用Objective-C,要是.mm就是Objective-C++,手动改变会让整个项目都用一种编译方式,很容易出错或者不兼容,比如NavigationItem实例化的时候就会出错,既然百度地图如此特立独行,那么最好的方式就是把地图相关的类改为.mm,其他的依旧,这样只有这个类会用Objective-C++编译方式。

 

如何显示地图并定位

要让车发动起来先得有引擎,所以在项目的根delegate类里就要通过BMKMapManager这个类来实现地图引擎的启动,具体代码:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions

{

// 要使用百度地图,请先启动BaiduMapManager

_mapManager = [[BMKMapManageralloc]init];

// 如果要关注网络及授权验证事件,请设定generalDelegate参数

BOOL ret = [_mapManagerstart:@"C5DCEBF3F591FCB69EE0A0B9B1BB4C948C3FA3CC"generalDelegate:nil];

if (!ret) {

NSLog(@”manager start failed!”);

}

self.window = [[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// Override point for customization afterapplication launch.

self.viewController = [[[ViewControlleralloc] initWithNibName:@”ViewController” bundle:nil]autorelease];

self.window.rootViewController =self.viewController;

[self.window makeKeyAndVisible];

return YES;

}

接下来要做的就是添加地图视图,在需要地图的类头文件里添加如下代码(这个类应该是.mm文件):

#import

#import “BMapKit.h”

@interface testViewController :UIViewController//两个协议要引入

{

BMKSearch* _search;//搜索要用到的

BMKMapView* mapView;//地图视图

IBOutlet UITextField* fromeText;

NSString  *cityStr;

NSString *cityName;

CLLocationCoordinate2D startPt;

float localLatitude;

float localLongitude;

BOOL localJudge;

NSMutableArray *pathArray;

}

@end

一些成员后面要用到先不提,这里只是实现地图的显示和定位,然后在.mm文件里,在@implementationtestViewController的前面添加这些代码

#import “testViewController.h”

#define MYBUNDLE_NAME @ “mapapi.bundle”

#define MYBUNDLE_PATH [[[NSBundlemainBundle] resourcePath] stringByAppendingPathComponent:MYBUNDLE_NAME]

#define MYBUNDLE [NSBundle bundleWithPath:MYBUNDLE_PATH]

BOOL isRetina = FALSE;

@interface RouteAnnotation :BMKPointAnnotation

{

int _type; ///<0:起点 1:终点 2:公交 3:地铁4:驾乘

int _degree;

}

@property (nonatomic) int type;

@property (nonatomic) int degree;

@end

@implementation RouteAnnotation

@synthesize type = _type;

@synthesize degree = _degree;

@end

@interface UIImage(InternalMethod)

-(UIImage*)imageRotatedByDegrees:(CGFloat)degrees;

@end

@implementation UIImage(InternalMethod)

-(UIImage*)imageRotatedByDegrees:(CGFloat)degrees

{

CGSize rotatedSize = self.size;

if (isRetina) {

rotatedSize.width *= 2;

rotatedSize.height *= 2;

}

UIGraphicsBeginImageContext(rotatedSize);

CGContextRef bitmap =UIGraphicsGetCurrentContext();

CGContextTranslateCTM(bitmap,rotatedSize.width/2, rotatedSize.height/2);

CGContextRotateCTM(bitmap, degrees * M_PI /180);

CGContextRotateCTM(bitmap, M_PI);

CGContextScaleCTM(bitmap, -1.0, 1.0);

CGContextDrawImage(bitmap,CGRectMake(-rotatedSize.width/2, -rotatedSize.height/2,rotatedSize.width, rotatedSize.height), self.CGImage);

UIImage* newImage =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;

}

@end

 有些代码对实现定位没有帮助,但是后面要用到,并且demo示例代码也是这么写的,所以引入了没有坏处,之后给这个类添加一个方法,获取图片资源用:

- (NSString*)getMyBundlePath1:(NSString*)filename

{

NSBundle * libBundle = MYBUNDLE ;

if ( libBundle && filename ){

NSString * s=[[libBundle resourcePath ]stringByAppendingPathComponent : filename];

NSLog ( @”%@” ,s);

return s;

}

return nil ;

}

下面才是真正添加地图的地方:

- (void)viewDidLoad

{

[super viewDidLoad];

mapView = [[BMKMapViewalloc]initWithFrame:CGRectMake(0, 92, 320, 388)];

[self.view addSubview:mapView];

mapView.delegate = self;

[mapViewsetShowsUserLocation:YES];//显示定位的蓝点儿

_search = [[BMKSearchalloc]init];//search类,搜索的时候会用到

_search.delegate = self;

fromeText.text=@”新中关”;

CGSize screenSize = [[UIScreen mainScreen]currentMode].size;

if ((fabs(screenSize.width – 640.0f) <0.1)

&& (fabs(screenSize.height –960.0f) < 0.1))

{

isRetina = TRUE;

}

pathArray=[[NSMutableArray array] retain]; //用来记录路线信息的,以后会用到

}

然后我在ib拖拽了几个按钮,功能显而易见,编译运行就应该成功了

 

如何定位获取经纬度和 如何通过定位得到城市,国家,街道等信息

这两个问题一段代码就可以解决,所以归并在一起,当添加了地图引擎和设定setShowsUserLocation:YES以后,地图已经在定位了,通过代理方法可以获得经纬度信息,并通过经纬度信息我可以获得街道城市等信息:

//百度定位获取经纬度信息

-(void)mapView:(BMKMapView *)mapViewdidUpdateUserLocation:(BMKUserLocation *)userLocation{

NSLog(@”!latitude!!! %f”,userLocation.location.coordinate.latitude);//获取经度

NSLog(@”!longtitude!!! %f”,userLocation.location.coordinate.longitude);//获取纬度

localLatitude=userLocation.location.coordinate.latitude;//把获取的地理信息记录下来

localLongitude=userLocation.location.coordinate.longitude;

CLGeocoder *Geocoder=[[CLGeocoderalloc]init];//CLGeocoder用法参加之前博客

CLGeocodeCompletionHandler handler =^(NSArray *place, NSError *error) {

for (CLPlacemark *placemark in place) {

cityStr=placemark.thoroughfare;

cityName=placemark.locality;

NSLog(@”city %@”,cityStr);//获取街道地址

NSLog(@”cityName %@”,cityName);//获取城市名

break;

}

};

CLLocation *loc = [[CLLocation alloc]initWithLatitude:userLocation.location.coordinate.latitudelongitude:userLocation.location.coordinate.longitude];

[Geocoder reverseGeocodeLocation:loccompletionHandler:handler];

}

其实,街道地址是不准确的,因为精度和纬度是不正确的,地址来源于经纬度,至于为什么不正确应该是因为”火星坐标系”,这里不做深入研究。
如何通过搜索地理名获得坐标
这个例子里是通过点击公交按钮,获得textfield里面内容地点到三里屯的公交路线信息,起点是textfield里面的内容,终点固定是三里屯。
事实上,获取路线需要得到起点的坐标,所以通过地理名获得坐标是后面获取路线的第一步。
在搜索的时候调用:

       BOOL flag = [_searchgeocode:fromeText.text withCity:cityStr];//这里用到之前定义的search了

if (!flag) {

NSLog(@”search failed”);

}

geocode第一个参数是fromeText,默认我写的是新中关,withCity是定位获得的城市,调用这个方法以后,结果会传给代理方法:

-(void)onGetAddrResult:(BMKAddrInfo*)resulterrorCode:(int)error{

NSLog(@”11111  %f”,result.geoPt.latitude);//获得地理名“新中关”的纬度

NSLog(@”22222  %f”,result.geoPt.longitude);//获得地理名“心中关”的经度

NSLog(@”33333 %@”,result.strAddr);//街道名

NSLog(@”4444%@”,result.addressComponent.province);//所在省份

NSLog(@”555%@”,result.addressComponent.city);

startPt = (CLLocationCoordinate2D){0,0};

startPt =result.geoPt;//把坐标传给startPt保存起来

}

这里可以看到好像百度地图api的结果都不是直接获得的,而是传给对应的代理方法。接下来的路线获取也一样

 

如何实现公交和驾车路线搜索

 

当textfield里面默认是新中关的时候,我的驾乘按钮代码如下:

-(IBAction)onClickDriveSearch

{

//清除之前的路线和标记

NSArray* array = [NSArrayarrayWithArray:mapView.annotations];

[mapView removeAnnotations:array];

array = [NSArrayarrayWithArray:mapView.overlays];

[mapView removeOverlays:array];

//清楚路线方案的提示信息

[pathArray removeAllObjects];

//如果是从当前位置为起始点

if (localJudge) {

BMKPlanNode* start = [[BMKPlanNodealloc]init];

startPt.latitude=localLatitude;

startPt.longitude=localLongitude;

start.pt = startPt;

start.name = cityStr;

BMKPlanNode* end = [[BMKPlanNodealloc]init];

end.name = @”三里屯”;

BOOL flag1 = [_searchdrivingSearch:cityName startNode:start endCity:@"北京市"endNode:end];

if (!flag1) {

NSLog(@”search failed”);

}

[start release];

[end release];

}else {

//如果从textfield获取起始点,不定位的话主要看这里

BOOL flag = [_search geocode:fromeText.textwithCity:cityStr];//通过搜索textfield地名获得地名的经纬度,之前已经讲过了,并存储在变量startPt里

if (!flag) {

NSLog(@”search failed”);

}

BMKPlanNode* start = [[BMKPlanNodealloc]init];

start.pt = startPt;//起始点坐标

start.name = fromeText.text;//起始点名字

BMKPlanNode* end = [[BMKPlanNodealloc]init];

end.name = @”三里屯”;//结束点名字

BOOL flag1 = [_searchdrivingSearch:cityName startNode:start endCity:@"北京市"endNode:end];//这个就是驾车路线查询函数,利用了startPt存储的起始点坐标,会调用代理方法onGetDrivingRouteResult

if (!flag1) {

NSLog(@”search failed”);

}

[start release];

[end release];

}

}

大致就是这样的逻辑,通过geocode函数得到地名坐标,然后通过drivingSearch函数设定起始点和结束点,并调用代理方法来利用这个路线结果,下面就是代理方法
//驾车的代理方法

-(void)onGetDrivingRouteResult:(BMKPlanResult*)resulterrorCode:(int)error

{

NSLog(@”onGetDrivingRouteResult:error:%d”,error);

if (error == BMKErrorOk) {

BMKRoutePlan* plan =(BMKRoutePlan*)[result.plans objectAtIndex:0];

RouteAnnotation* item = [[RouteAnnotationalloc]init];

item.coordinate = result.startNode.pt;

item.title = @”起点”;

item.type = 0;

[mapView addAnnotation:item];

[item release];

int index = 0;

int size = [plan.routes count];

for (int i = 0; i < 1; i++) {

BMKRoute* route = [plan.routesobjectAtIndex:i];

for (int j = 0; j < route.pointsCount;j++) {

int len = [route getPointsNum:j];

index += len;

}

}

BMKMapPoint* points = newBMKMapPoint[index];

index = 0;

for (int i = 0; i < 1; i++) {

BMKRoute* route = [plan.routesobjectAtIndex:i];

for (int j = 0; j < route.pointsCount;j++) {

int len = [route getPointsNum:j];

BMKMapPoint* pointArray =(BMKMapPoint*)[route getPoints:j];

memcpy(points + index, pointArray, len *sizeof(BMKMapPoint));

index += len;

}

size = route.steps.count;

for (int j = 0; j < size; j++) {

BMKStep* step = [route.stepsobjectAtIndex:j];

item = [[RouteAnnotation alloc]init];

item.coordinate = step.pt;

item.title = step.content;

item.degree = step.degree * 30;

item.type = 4;

[mapView addAnnotation:item];

[item release];

//把每一个步骤的提示信息存储到pathArray里,以后可以用这个内容实现文字导航

[pathArray addObject:step.content];

}

 

}

 

item = [[RouteAnnotation alloc]init];

item.coordinate = result.endNode.pt;

item.type = 1;

item.title = @”终点”;

[mapView addAnnotation:item];

[item release];

BMKPolyline* polyLine = [BMKPolylinepolylineWithPoints:points count:index];

[mapView addOverlay:polyLine];

delete []points;

//打印pathArray,检验获取的文字导航提示信息

for (NSString *string in pathArray) {

NSLog(@”patharray2 %@”,string);

}

}

 

}

这样驾车的路线就应该可以获取了,编译运行可以看到路线图,同时可以从打印的数组信息得到提示信息,内容就是地图上点击节点弹出的内容

同理,公交按钮的代码:

-(IBAction)onClickBusSearch

{

 

//清空路线

NSArray* array = [NSArrayarrayWithArray:mapView.annotations];

[mapView removeAnnotations:array];

array = [NSArrayarrayWithArray:mapView.overlays];

[mapView removeOverlays:array];

 

[pathArray removeAllObjects];

if (localJudge) {

//开始搜索路线,transitSearch调用onGetTransitRouteResult

BMKPlanNode* start = [[BMKPlanNodealloc]init];

startPt.latitude=localLatitude;

startPt.longitude=localLongitude;

start.pt = startPt;

start.name = cityStr;

BMKPlanNode* end = [[BMKPlanNodealloc]init];

end.name = @”三里屯”;

BOOL flag1 = [_searchtransitSearch:cityName startNode:start endNode:end];

if (!flag1) {

NSLog(@”search failed”);

}

[start release];

[end release];

}else{

//由textfield内容搜索,调用onGetAddrResult函数,得到目标点坐标startPt

BOOL flag = [_search geocode:fromeText.textwithCity:cityStr];

if (!flag) {

NSLog(@”search failed”);

}

//开始搜索路线,transitSearch调用onGetTransitRouteResult

BMKPlanNode* start = [[BMKPlanNodealloc]init];

start.pt = startPt;

start.name = fromeText.text;

BMKPlanNode* end = [[BMKPlanNodealloc]init];

end.name = @”三里屯”;

BOOL flag1 = [_searchtransitSearch:cityName startNode:startendNode:end];//公交路线对应的代理方法是onGetTransitRouteResult

if (!flag1) {

NSLog(@”search failed”);

}

[start release];

[end release];

 

}

}

 公交路线代理方法,这里和驾车路线不同的是,获取的提示信息不在一起,分上车信息和下车信息:

-(void)onGetTransitRouteResult:(BMKPlanResult*)resulterrorCode:(int)error

{

NSLog(@”onGetTransitRouteResult:error:%d”,error);

if (error == BMKErrorOk) {

BMKTransitRoutePlan* plan =(BMKTransitRoutePlan*)[result.plans objectAtIndex:0];

 

RouteAnnotation* item = [[RouteAnnotationalloc]init];

item.coordinate = plan.startPt;

item.title = @”起点”;

item.type = 0;

[mapView addAnnotation:item];

[item release];

item = [[RouteAnnotation alloc]init];

item.coordinate = plan.endPt;

item.type = 1;

item.title = @”终点”;

[mapView addAnnotation:item];

[item release];

 

int size = [plan.lines count];

int index = 0;

for (int i = 0; i < size; i++) {

BMKRoute* route = [plan.routesobjectAtIndex:i];

for (int j = 0; j < route.pointsCount;j++) {

int len = [route getPointsNum:j];

index += len;

}

BMKLine* line = [plan.linesobjectAtIndex:i];

index += line.pointsCount;

if (i == size – 1) {

i++;

route = [plan.routes objectAtIndex:i];

for (int j = 0; j < route.pointsCount;j++) {

int len = [route getPointsNum:j];

index += len;

}

break;

}

}

 

BMKMapPoint* points = newBMKMapPoint[index];

index = 0;

 

for (int i = 0; i < size; i++) {

BMKRoute* route = [plan.routesobjectAtIndex:i];

for (int j = 0; j < route.pointsCount;j++) {

int len = [route getPointsNum:j];

BMKMapPoint* pointArray =(BMKMapPoint*)[route getPoints:j];

memcpy(points + index, pointArray, len *sizeof(BMKMapPoint));

index += len;

}

BMKLine* line = [plan.linesobjectAtIndex:i];

memcpy(points + index, line.points,line.pointsCount * sizeof(BMKMapPoint));

index += line.pointsCount;

 

item = [[RouteAnnotation alloc]init];

item.coordinate =line.getOnStopPoiInfo.pt;

item.title = line.tip;

// NSLog(@”2222 %@”,line.tip);//上车信息,和下车信息加入数组的速度会配合,按顺序加入,不用考虑顺序问题

[pathArray addObject:line.tip];

if (line.type == 0) {

item.type = 2;

} else {

item.type = 3;

}

 

[mapView addAnnotation:item];

[item release];

route = [plan.routesobjectAtIndex:i+1];

item = [[RouteAnnotation alloc]init];

item.coordinate =line.getOffStopPoiInfo.pt;

item.title = route.tip;

// NSLog(@”2222 %@”,line.tip);

// NSLog(@”3333 %@”,item.title);//下车信息

[pathArray addObject:item.title];

if (line.type == 0) {

item.type = 2;

} else {

item.type = 3;

}

[mapView addAnnotation:item];

[item release];

if (i == size – 1) {

i++;

route = [plan.routes objectAtIndex:i];

for (int j = 0; j < route.pointsCount;j++) {

int len = [route getPointsNum:j];

BMKMapPoint* pointArray =(BMKMapPoint*)[route getPoints:j];

memcpy(points + index, pointArray, len *sizeof(BMKMapPoint));

index += len;

}

break;

}

}

 

BMKPolyline* polyLine = [BMKPolylinepolylineWithPoints:points count:index];

[mapView addOverlay:polyLine];

delete []points;

for (NSString *string in pathArray) {

NSLog(@”bus %@”,string);

}

}

}

注:火星坐标系统貌似只对获取的坐标有修正,对地图可见的内容,如大头针没有修正,测试可见路线起点是正确的定位位置,虽然由经纬度得到的街道信息确实是偏移的,可能地图又再次做了修正
关于从当前位置到目标的路线图,我做的处理就是定位按钮点击后,把现在位置的坐标传给了起始点然后在通过公交线路和驾车线路来实现线路的查询,总结完毕,以后更新。






目录

[隐藏]

简介

什么是百度地图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. BMKMapManager* _mapManager;
  5. }

在您的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. // 设置mapView的Delegate
  5. mapView.delegate = self;
  6. // 添加一个PointAnnotation
  7. BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
  8. CLLocationCoordinate2D coor;
  9. coor.latitude = 39.915;
  10. coor.longitude = 116.404;
  11. annotation.coordinate = coor;
  12. annotation.title = @"这里是北京";
  13. [mapView addAnnotation:annotation];
  14. }
  15. // Override
  16. - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
  17. {
  18. if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
  19. BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
  20. newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
  21. newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
  22. return newAnnotationView;
  23. }
  24. return nil;
  25. }

运行后,会在地图显示对应的标注点,点击会弹出气泡,效果如图:
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. // 设置delegate
  4. mapView.delegate = self;
  5. // 添加折线覆盖物
  6. CLLocationCoordinate2D coors[2] = {0};
  7. coors[0].latitude = 39.315;
  8. coors[0].longitude = 116.304;
  9. coors[1].latitude = 39.515;
  10. coors[1].longitude = 116.504;
  11. BMKPolyline* polyline = [BMKPolyline polylineWithCoordinates:coors count:2];
  12. [mapView addOverlay:polyline];
  13. }
  14. // Override
  15. - (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{
  16. if ([overlay isKindOfClass:[BMKPolyline class]]){
  17. BMKPolylineView* polylineView = [[[BMKPolylineView alloc] initWithOverlay:overlay] autorelease];
  18. polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];
  19. polylineView.lineWidth = 5.0;
  20. return polylineView;
  21. }
  22. return nil;
  23. }

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

添加多边形

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

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

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

添加圆

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

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

运行后,效果如图: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.  *@param result 搜索结果
  19.  *@param error 错误号,@see BMKErrorCode
  20.  */
  21. - (void)onGetDrivingRouteResult:(BMKPlanResult*)result errorCode:(int)error{
  22. }
  23. /**
  24.  *返回步行搜索结果
  25.  *@param result 搜索结果
  26.  *@param error 错误号,@see BMKErrorCode
  27.  */
  28. - (void)onGetWalkingRouteResult:(BMKPlanResult*)result errorCode:(int)error{
  29. }
  30. /**
  31.  *返回地址信息搜索结果
  32.  *@param result 搜索结果
  33.  *@param error 错误号,@see BMKErrorCode
  34.  */
  35. - (void)onGetAddrResult:(BMKAddrInfo*)result errorCode:(int)error{
  36. }
  37. /**
  38.  *返回公交详情搜索结果
  39.  *@param result 搜索结果
  40.  *@param error 错误号,@see BMKErrorCode
  41.  */
  42. - (void)onGetBusDetailResult:(BMKBusLineResult*)busLineResult errorCode:(int)error{
  43. }

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文件


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多