1.下载好百度地图sdk
2.基本环境配置
注册百度开放平台,申请地图对应的秘钥
申请密钥时注意要绑定正确的bundle Id 否则无法使用地图
ios9 使用 网络请求配置 在Info.plist中添加NSAppTransportSecurity
类型Dictionary
。在NSAppTransportSecurity
下添加NSAllowsArbitraryLoads
类型Boolean
,值设为YES
自iOS SDK v2.5.0起,为了对iOS8的定位能力做兼容,做了相应的修改,开发者在使用过程中注意事项如下: 需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):
NSLocationWhenInUseUsageDescription ,允许在前台使用时获取GPS的描述
NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述
需要在info.plist中添加:Bundle display name ,且其值不能为空
根据开发需要导入 .framework包
BaiduMapAPI_Base.framework为基础包,使用SDK任何功能都需导入,其他分包可按需导入。
百度地图SDK中提供了定位功能和动画效果,v2.0.0版本开始使用OpenGL渲染,因此您需要在您的Xcode工程中引入CoreLocation.framework和QuartzCore.framework、OpenGLES.framework、SystemConfiguration.framework、CoreGraphics.framework、Security.framework、libsqlite3.0.tbd(xcode7以前为 libsqlite3.0.dylib)、CoreTelephony.framework 、libstdc++.6.0.9.tbd(xcode7以前为libstdc++.6.0.9.dylib)。
(注:红色标识的系统库为v2.9.0新增的系统库,使用v2.9.0及以上版本的地图SDK,务必增加导入这3个系统库。)
在TARGETS->Build Settings->Other Linker
Flags 中添加-ObjC
选中工程名,在右键菜单中选择Add Files to “工程名”…,从BaiduMapAPI_Map.framework--Resources文件中选择mapapi.bundle文件,并勾选“Copy items if needed”复选框,单击“Add”按钮,将资源文件添加到工程中
在使用SDK的类 按需 引入下边的头文件:
(1).在appdelegate.h里添加 _mapManager属性,记得先引入所需头文件
@property (strong, nonatomic) BMKMapManager *mapManager;
(2).在appdelegate.m里添加对BMKMapManager的初始化,并填入您申请的授权Key
在application didFinishLaunchingWithOptions: 方法中实现:
// 要使用百度地图,请先启动BaiduMapManager
_mapManager = [[BMKMapManager alloc]init];
// 如果要关注网络及授权验证事件,请设定 generalDelegate参数
BOOL ret = [_mapManager start:@"RVvZ422wlB2DS4gGG0kSG7FZ" generalDelegate:nil];// 此处key申请时bundle id一定不能搞错,不然加载不出图来
if (!ret) {
NSLog(@"manager start failed!");
}
(3).在对应的调用地图的Controller里,引入所需头文件,接收bmkmapview代理,创建mapView属性
@property (nonatomic, strong) BMKMapView *mapView;
在view did load中实现:_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
[self.view addSubview:_mapView];
使用viewWillAppear、viewWillDisappear方法来控制BMKMapView的生命周期:
这样就已经实现了最基本的百度地图的调用了。