(iOS开发)百度地图SDK使用以及学习

注意: 由于Xcode 8 之后,访问定位需要权限,否则报错。在此贴上使用定位时的操作:

1、右击info.plist-->open as   Source Code  

2、添加:

<key>NSLocationUsageDescription</key>
	<string>App需要您的同意,才能访问位置</string>
	<key>NSLocationWhenInUseUsageDescription</key>
	<string>App需要您的同意,才能在使用期间访问位置</string>
	<key>NSLocationAlwaysUsageDescription</key>
	<string>App需要您的同意,才能始终访问位置</string>

一、环境的搭建

1、下载百度地图SDK,地址:百度地图sdk下载

2、注册账号、申请私钥:(注 :安全码就是 项目中的 Bundle Identifier)

3、将下面的framework导入到自己的工程中去:

4、还要引入一些需要的系统库文件:
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)。

5、在BaiduMapAPI_Map.framework下的Resources文件夹中将mapapi.bundle的图片资源库导入进自己的项目中,至此,环境搭建完毕


二、开始项目:

1、创建pch文件,在TARGETS 下的Build Settings 中的Prefix Header中,直接将pch文件拉入进去。

2、在pch文件中写入:

#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>//只引入所需的单个头文件
#import <BaiduMapAPI_Search/BMKRouteSearch.h>//路径规划


3、在Appdelegate.m中遵守一下<BMKGeneralDelegate>协议,并申明下BMKMapManager* _mapManager;

并写下方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 要使用百度地图,请先启动BaiduMapManager
    _mapManager = [[BMKMapManager alloc]init];
    // 如果要关注网络及授权验证事件,请设定    generalDelegate参数
    BOOL ret = [_mapManager start:@"在此处输入您的授权Key" generalDelegate:nil];

    if (!ret) {
        NSLog(@"manager start failed!");
    }
    UINavigationController * navi =[[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];
    self.window.rootViewController =navi;
    [self.window makeKeyAndVisible];
    return YES;
}

4、在ViewController中:遵循下代理: BMKMapViewDelegate

_mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, self.view.frame.size.height - 100)];
self.view = _mapView;

//遵循代理写在viewwillappear中
- (void)viewWillAppear:(BOOL)animated {
    [_mapView viewWillAppear];
    _mapView.delegate = self;
    _locService.delegate = self;
    _geoCodeSearch.delegate = self;
}

- (void)viewWillDisappear:(BOOL)animated {
    [_mapView viewWillDisappear];
    _mapView.delegate = nil;
    _locService.delegate = nil;
    _geoCodeSearch.delegate = nil;
}


5、添加定位功能 : 

1、申明 :BMKLocationService * _locService;//定位  

2、遵循代理:BMKLocationServiceDelegate

_locService =[[BMKLocationService alloc]init];

3、在viewDidload中添加定位的按钮:

 UIButton *positionBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    positionBtn.frame = CGRectMake(30, 64+64, 70, 20);
    [positionBtn setTitle:@"定位" forState:UIControlStateNormal];
    [positionBtn addTarget:self action:@selector(position:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:positionBtn];
    UIBarButtonItem *leftBarButon=[[UIBarButtonItem alloc]initWithCustomView:positionBtn];
    self.navigationItem.leftBarButtonItem=leftBarButon;

- (void)position:(UIButton *)btn
{
    _locService.delegate = self;
    //启动LocationService
    
    _mapView.zoomLevel = 14.1;
    _mapView.showsUserLocation = NO;//是否显示小蓝点,no不显示,我们下面要自定义的
    _mapView.userTrackingMode = BMKUserTrackingModeNone;
    //定位
    [_locService startUserLocationService];
    
    [_mapView removeAnnotation:_pointAnnotation];
    //添加大头针
    
    //    _pointAnnotation = [[BMKPointAnnotation alloc] init];
    //    _pointAnnotation.coordinate = _locService.userLocation.location.coordinate;
    //    _pointAnnotation.title = @"我在这个地方";
    //    _pointAnnotation.subtitle = @"你在哪呢";
    //    [_mapView addAnnotation:_pointAnnotation];
    //    [_mapView selectAnnotation:_pointAnnotation animated:YES];

}


4、实现BMKLocationServiceDelegate 代理方法

//点击定位之后获取的用户地址的代理方法
-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    BMKCoordinateRegion region;
    region.center.latitude  = userLocation.location.coordinate.latitude;
    region.center.longitude = userLocation.location.coordinate.longitude;
    region.span.latitudeDelta  = 0.0001;
    region.span.longitudeDelta = 0.0001;

    _mapView.centerCoordinate = userLocation.location.coordinate;//让地图的中心位置在这里
    [_mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];//出现动画效果
    //选择一个范围,让地图显示到当前界面
    [_mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
    [_mapView setZoomEnabled:YES];
    [_mapView setZoomEnabledWithTap:YES];
    [_mapView setZoomLevel:16.1];
    
    NSLog(@"定位的经度:%f,定位的纬度:%f",userLocation.location.coordinate.longitude,userLocation.location.coordinate.latitude);
    _mapView.showsUserLocation = NO;//显示用户位置
    [_mapView updateLocationData:userLocation];//更新用户位置
    [_locService stopUserLocationService];//停止获取用户的位置

    
    _pointAnnotation =[[BMKPointAnnotation alloc]init];
    _pointAnnotation.coordinate =userLocation.location.coordinate;
    _pointAnnotation.title =@"我在这呢";
    _pointAnnotation.subtitle =@"你在哪呢";
    [_mapView addAnnotation:_pointAnnotation];//运行的时候他会自动去寻找BMKAnnotationView,这个需要在标注代理方法中写。
    //也可以自定义BMKAnnotationView
    [_mapView selectAnnotation:_pointAnnotation animated:YES];
    
    
}


还必须要确定地图中心位置,否则,不好找出用户所在位置
_mapView.centerCoordinate = userLocation.location.coordinate(如果直接写在代理方法中,需要在代理方法末尾调用[_locService stopUserLocationService] 方法,让定位停止,要不然一直定位,你的地图就一直锁定在一个位置)。

注: 讲标注之前,需要弄懂两个类的区别,BMKPointAnnotation和BMKAnnotationView,很多初入地图行的人都弄不清(我自己一开始也是),前者官方解释的是一个点的标注,后者则是标注视图,好像还是不清楚0.0。。。。按照我的理解就是:前者代表一个点,比如你地图上有很多红色大头针,大头针往那里一插,是不是有个点?这个点,就表示BMKPointAnnotation,具有地理的经纬度特性(其他的一些信息也可以写在这里,如果你的后台将一系列信息包括经纬度信息传给你的话,你就可以重写这个类)。BMKAnnotationView则代表这个红色大头针,什么?红色大头针不好看,我要换成德玛西亚巨剑,好的,直接在这个BMKAnnotationView内部添加image即可,怎么样,弄懂了吗?? (转的一句话,很喜欢,哈哈。。)

原文链接: 点击打开链接

三、其他的一些功能,如:自定义标注,大头针的形状,POI检索,地理反编码,和路径规划等等功能,简书上比我写的好,传送门: 传送门来了。。


此博客只是写了最简单的应用和环境的搭建而已,详细功能可以观看上面的简书URL。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值