iOS百度地圖SDK集成詳細步驟


1.iOS百度地圖下載地址 http://developer.baidu.com/map/index.php?title=iossdk/sdkiosdev-download

根據需要選擇不同的版本  這里以自定義下載 開發包 為案例下載這個

2.下載得到一個名字為BaiduMap_IOSSDK_v2 的文件夾  將這個文件夾拖到自己的項目中

3.申請百度開發秘鑰 http://lbsyun.baidu.com/apiconsole/key 創建應用 填寫相應的東西即可,

注意安全碼要填你的應用bundle ID

4.注意事項 參考  http://developer.baidu.com/map/index.php?title=iossdk/guide/attention

在Info.plist文件中配置的時候 文件截圖 大家參考一下

注意在plist文件中添加一個Bundle display name   string類型   $(PRODUCT_NAME)  就是和Bundle name的名字一樣的 

5.添加 mapapi.bundle文件 

如果使用了基礎地圖功能,需要添加該資源,否則地圖不能正常顯示

mapapi.bundle中存儲了定位、默認大頭針標注View及路線關鍵點的資源圖片,還存儲了矢量地圖繪制必需的資源文件。如果您不需要使用內置的圖片顯示功能,則可以刪除bundle文件中的image文件夾。您也可以根據具體需求任意替換或刪除該bundle中image文件夾的圖片文件。

方法:選中工程名,在右鍵菜單中選擇Add Files to “工程名”…,從BaiduMapAPI.framework||Resources文件中選擇mapapi.bundle文件,並勾選“Copy items if needed”復選框,單擊“Add”按鈕,將資源文件添加到工程中。

6.將你項目中的隨便一個.m文件的后綴名變為.mm文件格式

例如這里將AppDelegate.m文件變為AppDelegate.mm文件格式

7.環境配置

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

8.添加相應的依賴庫

百度地圖SDK中提供了定位功能和動畫效果,v2.0.0版本開始使用OpenGL渲染,因此您需要在您的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)。

(注:紅色標識的系統庫為v2.9.0新增的系統庫,使用v2.9.0及以上版本的地圖SDK,務必增加導入這3個系統庫。)

添加方式:在Xcode的Project -> Active Target ->Build Phases ->Link Binary With Libraries,添加這幾個系統庫即可。

9.在AppDelegate.h文件中導入頭文件

在使用SDK的類 按需 引入下邊的頭文件:

#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>//引入周邊雷達功能所有的頭文件

 

</h//只引入所需的單個頭文件

 

在AppDelegate.h文件中創建地圖管理者,我把上述頭文件全部加到另一個頭文件中了

直接引用,創建百度地圖管理者

@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>

 

 

@property (strong, nonatomic) UIWindow *window;

/**

 *  百度地圖管理者

 */

@property (nonatomic, strong) BMKMapManager *mapManager;

 

@end

 

在AppDelegate.m文件中

 

/**

 *  定位

 */

@property (nonatomic, strong) BMKLocationService *locService;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { }

方法中加入

 

//百度地圖

   

//百度地圖

    // 要使用百度地圖,請先啟動BaiduMapManager

    self.mapManager = [[BMKMapManager alloc]init];

    // 如果要關注網絡及授權驗證事件,請設定     generalDelegate參數

    BOOL ret = [self.mapManager start:BMKKey  generalDelegate:nil];

    if (!ret) {

        YYCLog(@"百度地圖開啟失敗!");

        

        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"地圖定位開啟失敗!" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles: nil];

        [alert show];

    }else{

        

        self.locService =[[BMKLocationService alloc]init];

        self.locService.delegate = self;

        

        //定位的最小更新距離 100米

        self.locService.distanceFilter=20;

        

        //允許后台定位

        self.locService.allowsBackgroundLocationUpdates=NO;

        

        //啟動LocationService

        [self.locService startUserLocationService];

 

    }

 

#pragma mark - 存儲當前定位經緯度 存儲到本地

-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation

{

    [[NSUserDefaults standardUserDefaults] setFloat:userLocation.location.coordinate.latitude forKey:@"latitude"];

    [[NSUserDefaults standardUserDefaults] setFloat:userLocation.location.coordinate.longitude forKey:@"longitude"];

    

    YYCLog(@"latitude:%f,longitude:%f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);

    

    

    [self.locService stopUserLocationService];

}

 

 

//百度地圖兩個方法

- (void)onGetNetworkState:(int)iError

{

    if (0 == iError) {

        NSLog(@"聯網成功");

    }

    else{

        NSLog(@"onGetNetworkState %d",iError);

    }

    

}

 

- (void)onGetPermissionState:(int)iError

{

    if (0 == iError) {

        NSLog(@"授權成功");

    }

    else {

        NSLog(@"onGetPermissionState %d",iError);

    }

}

//百度地圖

- (void)applicationWillResignActive:(UIApplication *)application

{

    [BMKMapView willBackGround];//當應用即將后台時調用,停止一切調用opengl相關的操作

}

- (void)applicationDidBecomeActive:(UIApplication *)application

{

    [BMKMapView didForeGround];//當應用恢復前台狀態時調用,回復地圖的渲染和opengl相關的操作

}

要用的定位的話直接從本地取出就行了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM