(iOS开发)百度地图SDK使用以及学习
2016-10-26 14:37
579 查看
注意: 由于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文件中写入:
3、在Appdelegate.m中遵守一下<BMKGeneralDelegate>协议,并申明下BMKMapManager*
_mapManager;
并写下方法:
4、在ViewController中:遵循下代理: BMKMapViewDelegate
5、添加定位功能 :
1、申明 :BMKLocationService * _locService;//定位
2、遵循代理:BMKLocationServiceDelegate
3、在viewDidload中添加定位的按钮:
4、实现BMKLocationServiceDelegate 代理方法
还必须要确定地图中心位置,否则,不好找出用户所在位置
注:讲标注之前,需要弄懂两个类的区别,BMKPointAnnotation和BMKAnnotationView,很多初入地图行的人都弄不清(我自己一开始也是),前者官方解释的是一个点的标注,后者则是标注视图,好像还是不清楚0.0。。。。按照我的理解就是:前者代表一个点,比如你地图上有很多红色大头针,大头针往那里一插,是不是有个点?这个点,就表示BMKPointAnnotation,具有地理的经纬度特性(其他的一些信息也可以写在这里,如果你的后台将一系列信息包括经纬度信息传给你的话,你就可以重写这个类)。BMKAnnotationView则代表这个红色大头针,什么?红色大头针不好看,我要换成德玛西亚巨剑,好的,直接在这个BMKAnnotationView内部添加image即可,怎么样,弄懂了吗??
(转的一句话,很喜欢,哈哈。。)
原文链接:点击打开链接
三、其他的一些功能,如:自定义标注,大头针的形状,POI检索,地理反编码,和路径规划等等功能,简书上比我写的好,传送门:传送门来了。。
此博客只是写了最简单的应用和环境的搭建而已,详细功能可以观看上面的简书URL。
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 <BaiduMapA 4000 PI_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。
相关文章推荐
- ios开发网络学习二:URL转码以及字典转模型框架MJExtension的使用
- iOS学习——如何在mac上获取开发使用的模拟器的资源以及模拟器中每个应用的应用沙盒
- 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法 推荐
- IOS开发学习一 :Cocoa环境熟悉和Xcode使用
- api工程IOS学习:在IOS开发中使用GoogleMaps SDK
- 【Cocos2d-X开发学习笔记】第18期:动作类之改变动作对象、函数回调动作以及过程动作的使用
- IOS开发学习笔记(十七)——使用地理位置及地图(中篇)
- iOS开发学习笔记:使用xcode里的单元测试,放在STAssert…里面的语句无法使用自动完成功能
- iOS开发 or xCode 使用 学习备注(不断完善)
- IOS开发学习笔记(十一)——ObjectC中集合类型的使用
- 【Android开发学习44】android: 静态XML和动态加载XML混合使用,以及重写Layout控件
- 【Cocos2d-X开发学习笔记】第18期:动作类之改变动作对象、函数回调动作以及过程动作的使用
- 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法
- iOS开发证书和Profile文件 介绍以及使用
- Flash开发iOS应用全攻略(三)——如何使用iOS开发者授权以及如何申请证书
- 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法
- Flash开发iOS应用全攻略(三)——如何使用iOS开发者授权以及如何申请证书
- IOS 开发学习(1): 学习纲领以及Objective-C 基本语法学习
- IOS开发学习笔记(十八)——使用地理位置及地图(下篇)
- IOS开发学习笔记(十六)——使用地理位置及地图(上篇)