1. 程式人生 > >ios 整合百度地圖(獲取定位,反向地理編碼)

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開發指南