iOS百度地图API的使用

(一)、开发前准备工作:
<1.开发者可在百度地图iOS SDK的下载页面下载到最新版的地图SDK,下载地址为:http://developer.baidu.com/map/index.php?title=iossdk/sdkiosdev-download

<2.用户在使用SDK之前需要获取百度地图移动版开发密钥(Key),该key与你的百度账户相关联。

您必须先有百度帐户,才能获得key。并且,该key与您引用SDK的程序包名有关,具体流程请参照申请密钥。请妥善保存Key,地图初始化时需要用到Key。

百度地图iOS SDK开发密钥的申请地址为:http://lbsyun.baidu.com/apiconsole/key

❤️.由于iOS9改用更安全的https,为了能够在iOS9中正常使用地图SDK,请在"Info.plist"中进行如下配置,否则影响SDK的使用。
添加下面的内容,就能保证Xcode正常联网

<4.如果在iOS9中使用了调起百度地图客户端功能,必须在"Info.plist"中进行如下配置,否则不能调起百度地图客户端。
这里写图片描述

<5.管理地图的生命周期:自2.0.0起,BMKMapView新增viewWillAppear、viewWillDisappear方法来控制BMKMapView的生命周期,并且在一个时刻只能有一个BMKMapView接受回调消息,因此在使用BMKMapView的viewController中需要在viewWillAppear、viewWillDisappear方法中调用BMKMapView的对应的方法,并处理delegate,代码如下:

-(void)viewWillAppear:(BOOL)animated
{
[_mapView viewWillAppear];
_mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放
}
-(void)viewWillDisappear:(BOOL)animated
{
[_mapView viewWillDisappear];
_mapView.delegate = nil; // 不用时,置nil
}
<6.自iOS SDK v2.5.0起,为了对iOS8的定位能力做兼容,做了相应的修改,开发者在使用过程中注意事项如下: 需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):

NSLocationWhenInUseUsageDescription ,允许在前台使用时获取GPS的描述

NSLocationAlwaysUsageDescription ,允许永久使用GPS的描述

<7.在使用Xcode6进行SDK开发过程中,需要在info.plist中添加:Bundle display name ,且其值不能为空(Xcode6新建的项目没有此配置,若没有会造成manager start failed)
这里写图片描述

<8.手动配置.framework形式开发包

第一步、根据需要导入 .framework包

百度地图 iOS SDK 采用分包的形式提供 .framework包,请广大开发者使用时确保各分包的版本保持一致。其中BaiduMapAPI_Base.framework为基础包,使用SDK任何功能都需导入,其他分包可按需导入。

将所需的BaiduMapAPI_**.framework拷贝到工程所在文件夹下。

在 TARGETS->Build Phases-> Link Binary With Libaries中点击“+”按钮,在弹出的窗口中点击“Add Other”按钮,选择BaiduMapAPI_**.framework添加到工程中。

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

第二步、引入所需的系统库

百度地图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个系统库。)

添加方式:在Xcode的Project -> Active Target ->Build Phases ->Link Binary With Libraries,添加这几个系统库即可。

第三步、环境配置

在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC。

第四步、引入mapapi.bundle资源文件

如果使用了基础地图功能,需要添加该资源,否则地图不能正常显示mapapi.bundle中存储了定位、默认大头针标注View及路线关键点的资源图片,还存储了矢量地图绘制必需的资源文件。如果您不需要使用内置的图片显示功能,则可以删除bundle文件中的image文件夹。您也可以根据具体需求任意替换或删除该bundle中image文件夹的图片文件。

方法:选中工程名,在右键菜单中选择Add Files to “工程名”…,从BaiduMapAPI_Map.framework||Resources文件中选择mapapi.bundle文件,并勾选“Copy items if needed”复选框,单击“Add”按钮,将资源文件添加到工程中。

第五步、引入头文件

在使用SDK的类 按需 引入下边的头文件:

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件

#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件

#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云检索功能所有的头文件

#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件

#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入计算工具所有的头文件

#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周边雷达功能所有的头文件

#import < BaiduMapAPI_Map/BMKMapView.h>//只引入所需的单个头文件
(二)、工程开始
<1.Appdelegate.m文件代码如下
#import “AppDelegate.h”
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件

@interface AppDelegate ()<UIApplicationDelegate,BMKGeneralDelegate>
{
BMKMapManager * _mapManager;
}
@end

@implementation AppDelegate

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    _mapManager = [[BMKMapManager alloc] init];
    BOOL ret = [_mapManager start:@“您申请的秘钥” generalDelegate:self];
    if (!ret) {
    NSLog(@“秘钥错误”);
    }

    self.firstVC = [[ViewController alloc] init];
    self.firstVC.tabBarItem.title = @“定位”;

    self.secondVC = [[SecondViewController alloc] init];
    self.secondVC.tabBarItem.title = @“检索”;

    UITabBarController *tab = [[UITabBarController alloc] init];
    tab.viewControllers = @[self.firstVC,self.secondVC];

    self.window.rootViewController = tab;

    return YES;
    }

//判断有没有连入百度的服务器

  • (void)onGetNetworkState:(int)iError
    {
    if (0 == iError) {
    NSLog(@“联网成功”);
    }
    else{
    NSLog(@“onGetNetworkState %d”,iError);
    }

}

//判断我们的key是否可用

  • (void)onGetPermissionState:(int)iError
    {
    if (0 == iError) {
    NSLog(@“授权成功”);
    }
    else {
    NSLog(@“onGetPermissionState %d”,iError);
    }
    }
    <2.ViewController.mm中文件如下
    备注:因为静态库中采用ObjectC++实现,因此需要您保证您工程中至少有一个.mm后缀的源文件(您可以将任意一个.m后缀的文件改名为.mm)。本类中实现基础地图使用和定位。代码如下:
    #import “ViewController.h”
    #import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地图功能所有的头文件
    #import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件

@interface ViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>
{

BMKMapView *_mapView;
BMKLocationService *locationService;

}
@end

BOOL weixingOrzhengchang;

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];

    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
    _mapView.mapType = BMKMapTypeStandard;
    _mapView.delegate = self;
    //比例尺
    _mapView.zoomLevel = 17;

    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn1.frame = CGRectMake(10, 20, 170, 30);
    btn1.backgroundColor = [UIColor lightGrayColor];
    [btn1 setTitle:@“地图模式” forState:UIControlStateNormal];

    btn1.showsTouchWhenHighlighted = YES;

    [btn1 addTarget:self action:@selector(XJBtnEvent) forControlEvents:UIControlEventTouchUpInside];

    [_mapView addSubview:btn1];

    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(190, 20, 170, 30);
    btn2.backgroundColor = [UIColor lightGrayColor];
    [btn2 setTitle:@“定位” forState:UIControlStateNormal];
    [btn2 addTarget:self action:@selector(locationBtnEvent) forControlEvents:UIControlEventTouchUpInside];
    [_mapView addSubview:btn2];

    [self.view addSubview:_mapView];
    //初始化BMKLocationSercvice;
    locationService = [[BMKLocationService alloc] init];
    locationService.delegate = self;
    locationService.distanceFilter = 10;
    }

//切换卫星还是地图

  • (void)XJBtnEvent
    {
    if (weixingOrzhengchang == YES) {
    _mapView.mapType = BMKMapTypeSatellite;
    weixingOrzhengchang = NO;
    }else{
    _mapView.mapType = BMKMapTypeStandard;
    weixingOrzhengchang = YES;
    }
    }

//定位实现的相关方法

  • (void)locationBtnEvent
    {
    //触发定位
    [locationService startUserLocationService];
    //让地图跳转到定好位的位置
    _mapView.showsUserLocation = YES;
    }

  • (void)willStartLocatingUser
    {
    NSLog(@“开始定位”);
    }

  • (void)didFailToLocateUserWithError:(NSError *)error
    {
    NSLog(@“定位失败%@”,error);
    }

//处理方向变更信息

  • (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
    {
    NSLog(@“heading is %@”,userLocation.heading);
    }
    //处理位置坐标更新

  • (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    //模拟器没有(定位,照相机,重力感应,陀螺仪,不支持网络推送)

    // //动态更新我的位置数据
    // [mapView updateLocationData:userLocation];
    //大头针
    BMKPointAnnotation *annotation = [[BMKPointAnnotation alloc]init];
    //设置大头针的坐标(coordinate2d坐标)
    annotation.coordinate = userLocation.location.coordinate;
    //2d坐标的内容
    annotation.title = @“谢哥”;
    //添加到地图里
    [_mapView addAnnotation:annotation];
    //设定地图中心点坐标
    [_mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    //打印经纬度
    NSLog(@“didUpdateUserLocation lat %f,long %f”,userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    }

  • (void)viewWillAppear:(BOOL)animated
    {
    [_mapView viewWillAppear];
    // _mapView.delegate = nil; // 此处记得不用的时候需要置nil,否则影响内存的释放
    }
    -(void)viewWillDisappear:(BOOL)animated
    {
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时,置nil
    locationService.delegate = nil;
    }

  • (void)dealloc
    {
    if (_mapView) {
    _mapView = nil;
    }
    if (locationService) {
    locationService = nil;
    }
    }

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

  • (BOOL)prefersStatusBarHidden
    {
    return YES;
    }
    ❤️.SecondViewController.m中代码如下(本类中实现了正向地理编码和反向地理编码)
    #import “SecondViewController.h”
    //地图
    #import <BaiduMapAPI_Map/BMKMapComponent.h>
    //地理编码
    #import <BaiduMapAPI_Search/BMKSearchComponent.h>

@interface SecondViewController ()<BMKGeoCodeSearchDelegate,BMKMapViewDelegate>
{
BMKGeoCodeSearch *_searcher;//搜索功能对象
BMKMapView *_mapView;//地图
}
///城市-输入框
@property (strong, nonatomic) UITextField *chengshiField;
///地址-输入框
@property (strong, nonatomic) UITextField *dizhiField;
///经度-输入框
@property (strong, nonatomic) UITextField *jingduField;
///纬度-输入框
@property (strong, nonatomic) UITextField *weiduField;
///正向地理编码按钮
@property (strong, nonatomic) UIButton *zhengxiangBtn;
///反向地理编码按钮
@property (strong, nonatomic) UIButton *fanxiangBtn;

@end

@implementation SecondViewController

  • (void)viewWillAppear:(BOOL)animated{
    [_mapView viewWillAppear];
    }

  • (void)viewDidLoad {
    [super viewDidLoad];
    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, 375, 667)];
    _mapView.mapType = BMKMapTypeStandard;
    _mapView.delegate = self;
    //比例尺
    _mapView.zoomLevel = 17;

    [self.view addSubview:_mapView];
    //构建搜索
    _searcher = [[BMKGeoCodeSearch alloc] init];
    _searcher.delegate = self;

    [self createUI];
    }

  • (void)createUI{
    self.chengshiField = [[UITextField alloc]initWithFrame:CGRectMake(10, 30, 100, 30)];
    self.chengshiField.placeholder = @“请输入城市”;
    self.chengshiField.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.chengshiField];

    self.dizhiField = [[UITextField alloc]initWithFrame:CGRectMake(120, 30, 100, 30)];
    self.dizhiField.placeholder = @“请输入地址”;
    self.dizhiField.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.dizhiField];

    self.jingduField = [[UITextField alloc]initWithFrame:CGRectMake(10, 70, 100, 30)];
    self.jingduField.placeholder = @“请输入经度”;
    self.jingduField.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.jingduField];

    self.weiduField = [[UITextField alloc]initWithFrame:CGRectMake(120, 70, 100, 30)];
    self.weiduField.placeholder = @“请输入纬度”;
    self.weiduField.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.weiduField];

    self.zhengxiangBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.zhengxiangBtn.backgroundColor = [UIColor orangeColor];
    [self.zhengxiangBtn setTitle:@“正向地理编码” forState:UIControlStateNormal];
    self.zhengxiangBtn.frame = CGRectMake(250, 30, 120, 30);
    [self.zhengxiangBtn addTarget:self action:@selector(zhengxiangAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.zhengxiangBtn];

    self.fanxiangBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.fanxiangBtn.backgroundColor = [UIColor orangeColor];
    [self.fanxiangBtn setTitle:@“反向地理编码” forState:UIControlStateNormal];
    self.fanxiangBtn.frame = CGRectMake(250, 70, 120, 30);
    [self.fanxiangBtn addTarget:self action:@selector(fanxiangAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.fanxiangBtn];
    }
    //正向地理编码事件

  • (void)zhengxiangAction{
    [self.dizhiField resignFirstResponder];
    [self.chengshiField resignFirstResponder];

    BMKGeoCodeSearchOption *geoCodesearchOption = [[BMKGeoCodeSearchOption alloc] init];
    geoCodesearchOption.city = self.chengshiField.text;
    geoCodesearchOption.address = self.dizhiField.text;
    BOOL flag = [_searcher geoCode:geoCodesearchOption];
    // [geoCodesearchOption release];
    if (flag == YES) {
    NSLog(@“geo检索发送成功”);
    }
    else
    {
    NSLog(@“geo检索发送失败”);
    }
    }

  • (void)fanxiangAction{
    //发起反向地理编码检索
    CLLocationCoordinate2D pt = (CLLocationCoordinate2D){39.915, 116.404};
    BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[
    BMKReverseGeoCodeOption alloc]init];
    reverseGeoCodeSearchOption.reverseGeoPoint = pt;
    BOOL flag = [_searcher reverseGeoCode:reverseGeoCodeSearchOption];
    // [reverseGeoCodeSearchOption release];
    if(flag)
    {
    NSLog(@“反geo检索发送成功”);
    }
    else
    {
    NSLog(@“反geo检索发送失败”);
    }
    }

  • (void)onGetGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error{
    if (error == BMK_SEARCH_NO_ERROR) {
    NSLog(@“经度%f,纬度%f”,result.location.latitude,result.location.longitude);
    NSLog(@"%@",result.address);
    _mapView.centerCoordinate = result.location;
    _mapView.zoomLevel = 17;
    }
    else{
    NSLog(@“抱歉,未找到结果”);
    }
    }

//接收反向地理编码结果
-(void) onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:
(BMKReverseGeoCodeResult *)result
errorCode:(BMKSearchErrorCode)error{
if (error == BMK_SEARCH_NO_ERROR) {
// 在此处理正常结果
}
else {
NSLog(@“抱歉,未找到结果”);
}
}

  • (void)viewWillDisappear:(BOOL)animated{
    _mapView.delegate = nil;
    _searcher.delegate = nil;
    }

  • (void)dealloc{
    if (_mapView) {
    _mapView = nil;
    }
    }

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值