美文网首页码农的日常之iOS开发iOS Developer
iOS 百度SDK获取当前位置信息

iOS 百度SDK获取当前位置信息

作者: Me小酥酥 | 来源:发表于2016-06-30 17:57 被阅读8249次

    关于百度地图SDK的使用网上太多了,本人也是在实际开发中碰到了这样的需求所以仔细看了SDK开发文档,又在网上借鉴了些资料。黄天不负有心人,终于弄出来了,今天在此记录下。

    准备工作:下载地图SDK

    一、申请密钥

    申请密钥百度地图开发者文档写的很详细了,在这里我就不废话了直接上链接  申请密钥

    二、配置开发环境

    1、手动导入.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 Setting 中找到 Compile Sources As,并将其设置为"Objective-C++"

    2、引入系统库

    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)。

    3、配置环境

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

    4、引入mapapi.bundle资源文件

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

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

    5、更改info.plist

    iOS9改用更安全的https,为了能够在iOS9中正常使用地图SDK,需要在"Info.plist"中进行如下配置,否则影响SDK的使用。

    需要在info.plist里添加(以下二选一,两个都添加默认使用NSLocationWhenInUseUsageDescription):

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

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

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

    三、添加代码

    1、初始化BMKMapManager

    在AppDelegate.h中添加

    #import<BaiduMapAPI_Base/BMKBaseComponent.h>

    #import<BaiduMapAPI_Base/BMKMapManager.h>

    @interface AppDelegate : UIResponder

    {

    BMKMapManager* _mapManager;

    }

    @property (strong, nonatomic) UIWindow *window;

    @end

    在AppDelegate.m的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions中添加

    _mapManager = [[BMKMapManager alloc]init];

    BOOL ret = [_mapManager start:@"在此处输入您的授权Key"  generalDelegate:nil];

    if (!ret) {

    NSLog(@"manager start failed!");

    }

    在需要用的的ViewController中使用

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

    #import<BaiduMapAPI_Map/BMKMapView.h>

    #import<BaiduMapAPI_Location/BMKLocationService.h>

    #import<BaiduMapAPI_Search/BMKGeocodeSearch.h>

    #import<BaiduMapAPI_Map/BMKMapComponent.h>

    #import<BaiduMapAPI_Search/BMKPoiSearchType.h>

    需要添加代理

    BMKMapViewDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate

    {

           BMKMapView *_mapView;  //地图

           BMKLocationService *_locService;  //定位

           BMKGeoCodeSearch *_geocodesearch; //地理编码主类,用来查询、返回结果信息

    }

    -(void)viewWillAppear:(BOOL)animated

    {

    [_mapView viewWillAppear];

    _mapView.delegate = self; // 此处记得不用的时候需要置nil,否则影响内存的释放

    }

    -(void)viewWillDisappear:(BOOL)animated

    {

    [_mapView viewWillDisappear];

    _mapView.delegate = nil; // 不用时,置nil

    }

    - (void)viewDidLoad {

    [super viewDidLoad];

    //添加地图视图

    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64)];

    _mapView.showsUserLocation = YES; //是否显示定位图层(即我的位置的小圆点)

    _mapView.zoomLevel = 19;//地图显示比例

    _mapView.mapType = BMKMapTypeStandard;//设置地图为空白类型

    [self.view addSubview:_mapView];

    //

    _geocodesearch = [[BMKGeoCodeSearch alloc] init];

    _geocodesearch.delegate = self;

    [self startLocation];//开始定位方法

    }

    //开始定位

    -(void)startLocation

    {

    //初始化BMKLocationService

    _locService = [[BMKLocationService alloc]init];

    _locService.delegate = self;

    _locService.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;

    //启动LocationService

    [_locService startUserLocationService];

    }

    //实现相关delegate 处理位置信息更新

    //处理方向变更信息

    - (void)didUpdateUserHeading:(BMKUserLocation *)userLocation

    {

    NSLog(@"heading is %@",userLocation.heading);

    }

    //处理位置坐标更新

    - (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation

    {

    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

    //普通态

    //以下_mapView为BMKMapView对象

    [_mapView updateLocationData:userLocation]; //更新地图上的位置

    _mapView.centerCoordinate = userLocation.location.coordinate; //更新当前位置到地图中间

    //地理反编码

    BMKReverseGeoCodeOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeOption alloc]init];

    reverseGeocodeSearchOption.reverseGeoPoint = userLocation.location.coordinate;

    BOOL flag = [_geocodesearch reverseGeoCode:reverseGeocodeSearchOption];

    if(flag){

    NSLog(@"反geo检索发送成功");

    [_locService stopUserLocationService];

    }else{

    NSLog(@"反geo检索发送失败");

    }

    }

    #pragma mark -------------地理反编码的delegate---------------

    -(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher result:(BMKReverseGeoCodeResult *)result errorCode:(BMKSearchErrorCode)error

    {

    NSLog(@"address:%@----%@",result.addressDetail,result.address);

    //addressDetail:     层次化地址信息

    //address:    地址名称

    //businessCircle:  商圈名称

    // location:  地址坐标

    //  poiList:   地址周边POI信息,成员类型为BMKPoiInfo

    //定位失败

    - (void)didFailToLocateUserWithError:(NSError *)error{

    NSLog(@"error:%@",error);

    }

    终于写完了,其实弄过一次就没有感觉太复杂了,希望可以帮到有需要的朋友。

    相关文章

      网友评论

      • 弹一首键盘协奏曲:点击个人位置时候崩溃 遇到过没?
      • 寒枫林:不错
      • 羊村里的羊:代理方法不走怎么搞?
      • assassinate:这代理 方法不走 几个意思
      • 会跳舞的狮子:可以的 公司突然用百度的 高德的简单多了
      • 九剑仙:very good...thinks
      • e54b69cb7854:有用,学习中,谢谢:grin:
      • 低头敲代码的小猿:请问一下,如果说突然之间就不走地理反编码的代理方法了,那么哪里出现问题的可能性会比较大?
        哈利_:看看传进去的经纬度是否正确.
        这个lat和lng 要用大括号括起来, 声明是这样的:
        CLLocationDegrees lat = userLocation.location.coordinate.latitude;
        CLLocationDegrees lng = userLocation.location.coordinate.longitude;

        CLLocationCoordinate2D pt = (CLLocationCoordinate2D){lat, lng}; <------这块要注意
        BMKReverseGeoCodeOption *reverseGeoCodeSearchOption = [[
        BMKReverseGeoCodeOption alloc]init];
        reverseGeoCodeSearchOption.reverseGeoPoint = pt;
        低头敲代码的小猿:@assassinate 我是因为地址模糊,加了判断就好了!
        assassinate:解决了吗,我也 碰到 这个问题

      本文标题:iOS 百度SDK获取当前位置信息

      本文链接:https://www.haomeiwen.com/subject/tzqfjttx.html