ios 集成百度地图(获取定位,反向地理编码)

原因 :之所以使用百度地图,是因为当时使用苹果自带的定位给后台传经纬度,后台用百度解析误差比较大,所以换成了百度地图 本文主要讲解下cocoapods 集成 看详细集成请点击(cocoapod集成链接)
1 、首先去百度地图开放平台注册账户
在这里插入图片描述
点击立即使用
在这里插入图片描述
然后注册
在这里插入图片描述 2、然后进行邮箱激活 ,申请秘钥 进入这个界面
在这里插入图片描述
在这里插入图片描述
红色框部分看项目需要哪些功能 选择哪些 然后提交在这里插入图片描述
3、使用百度sdk
1) 首先在info.plist 文件中加入
在这里插入图片描述
2) 在appdelegate中文件配置
首先引入头文件

#import <BaiduMapAPI_Base/BMKBaseComponent.h>
#define BMK_KEY  @"qqdGrSQf0lOs2NuBRZ8iFqiWwc7G51LN"
  BMK_KEY对应的值是申请的秘钥
  1. 然后声明一个属性
@interface AppDelegate ()
@property (nonatomic, strong) BMKMapManager *mapManager;
@end

4 )在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions ;方法里面初始化百度sdk

//百度地图
    _bmapManager = [BMKMapManager new];
    BOOL ret = [_bmapManager start:BMK_KEY generalDelegate:nil];
    if (!ret){
        NSLog(@"百度地图启动失败");
    }else{
        NSLog(@"百度地图启动成功");
    }

5) 看自己项目在那个VC中使用地图
首先 导入相应的头文件

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入检索功能所有的头文件
#import <BaiduMapAPI_Search/BMKGeocodeSearchOption.h>
#import <BaiduMapAPI_Search/BMKGeocodeSearchResult.h>

6) 声明一个属性 并引入代理

@interface ViewController ()<CLLocationManagerDelegate,BMKGeneralDelegate,BMKLocationServiceDelegate,BMKGeoCodeSearchDelegate>//BMKReverseGeoCodeOption
@property (nonatomic, strong) BMKLocationService *locationService;
@property (nonatomic, strong) BMKGeoCodeSearch * geocodeSearch;
@end

7) 在viewDidLoad 方法中 启动定位服务

[self.locationService startUserLocationService];//启动定位服务

8)实现百度地图相关代理方法 和 初始化的方法

#pragma mark - LocationDelegate 百度地图代理
/**
 *定位失败后,会调用此函数
 *@param error 错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"地图定位失败======%@",error);
}

//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,
          userLocation.location.coordinate.longitude);
    if ((userLocation.location.coordinate.latitude != 0 || userLocation.location.coordinate.longitude != 0))
    {
        NSString *latitude = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.latitude];
        NSString *longitude = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.longitude];
        NSLog(@"didUpdateUserLocation lat %@,long %@",latitude,
              longitude);
        [self reverseGeoCodeWithLatitude:latitude withLongitude:longitude];
    }else{
        NSLog(@"失败");
    }
}

//地图定位
- (BMKLocationService *)locationService
{
    if (!_locationService)
    {
        _locationService = [[BMKLocationService alloc] init];
        _locationService.delegate = self;
    }
    return _locationService;
}


//检索对象
- (BMKGeoCodeSearch *)geocodeSearch
{
    if (!_geocodeSearch)
    {
        _geocodeSearch = [[BMKGeoCodeSearch alloc] init];
        _geocodeSearch.delegate = self;
    }
    return _geocodeSearch;
}

#pragma mark ----反向地理编码
- (void)reverseGeoCodeWithLatitude:(NSString *)latitude withLongitude:(NSString *)longitude
{
    
    //发起反向地理编码检索
    CLLocationCoordinate2D coor;
    coor.latitude = [latitude doubleValue];
    coor.longitude = [longitude doubleValue];
    BMKReverseGeoCodeSearchOption *reverseGeocodeSearchOption = [[BMKReverseGeoCodeSearchOption alloc] init];
    reverseGeocodeSearchOption.location = coor;
    BOOL flag = [self.geocodeSearch reverseGeoCode:reverseGeocodeSearchOption];;
    if (flag)
    {
        NSLog(@"反地理编码成功");
    }
    else
    {
        NSLog(@"反地理编码失败");
    }
}


//发送成功,百度将会返回东西给你
-(void)onGetReverseGeoCodeResult:(BMKGeoCodeSearch *)searcher
                          result:(BMKReverseGeoCodeSearchResult *)result
                       errorCode:(BMKSearchErrorCode)error
{
    if (error == BMK_SEARCH_NO_ERROR) {
        NSString *address = result.address;  //地址名称
        NSString *sematicDescription = result.sematicDescription;  //结合当前位置POI的语义化结果描述
        NSString *businessCircle = result.businessCircle;  //商圈名称
        BMKAddressComponent *addressDetail = result.addressDetail;  //层次化地址信息
        NSArray *poiList = result.poiList;  //地址周边POI信息,成员类型为BMKPoiInfo
        NSLog(@"我的位置在 %@ 结合当前位置POI的语义化结果描述:%@ 商圈名称:%@ 层次化地址信息:%@ 地址周边POI信息,成员类型为BMKPoiInfo:%@",address,sematicDescription,businessCircle,addressDetail,poiList);
                [self.locationService stopUserLocationService]; //定位取消
    }
}

完成运行 成功
在这里插入图片描述
可能的问题

1、 会报如下问题

在这里插入图片描述
解决 在info.plist 中加入 在这里插入图片描述

2、百度地图反geo检索发送失败

可能因为,key是其它样例Demo的,或者以前申请的过期了。检查一下,用自己工程的Bundle Identifer重新申请key,在真机上进行测试,反检索发起成功。这时候需要重新申请密钥key。

demo地址
资料 百度sdk开发指南

猜你喜欢

转载自blog.csdn.net/u013983033/article/details/83062861