您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 學習:在工程中整合百度地圖 SDK

編輯:iOS 開發綜合

要在 iOS 應用中加入地圖功能,百度地圖 SDK 是一個不錯的選擇。以下記錄完整的整合流程,從靜態庫的引入到 AppDelegate 的初始化設定。

一、加入 SDK 檔案

將下載的 SDK 中的 inc 資料夾、mapapi.bundle 與 libbaidumapapi.a 加入工程。其中 libbaidumapapi.a 有兩個版本,分別對應模擬器與真機,可採用以下三種方式之一:

第一種:直接把對應平台的 .a 檔拖進 Xcode 左側的 Groups & Files 中。缺點是每次在真機與模擬器切換編譯時都得重新加入。

第二種:用 lipo 指令把兩份 .a 合併成通用檔,之後只需加入這一個檔案即可:

lipo –create Release-iphoneos/libbaidumapapi.a Release-iphonesimulator/libbaidumapapi.a –output libbaidumapapi.a

第三種:把 API 的 libs 資料夾複製到應用工程根目錄,接著在 Build 的 Linking → Other Linker Flags 中加入 -ObjC,並在 Search Path → Library Search Paths 中設定靜態庫的鏈接路徑,例如 "$(SRCROOT)/../libs/Release$(EFFECTIVE_PLATFORM_NAME)",其中 $(SRCROOT) 代表工程目錄,$(EFFECTIVE_PLATFORM_NAME) 代表當前配置是 OS 還是 simulator。

筆者採用第二種方法,在真機與模擬器下都能正常除錯。

二、確保有 .mm 檔

由於靜態庫是以 Objective-C++ 實作,工程中至少要有一個 .mm 檔(可把 appdelegate.m 改名為 .mm)。

三、導入所需框架

需加入 CoreLocation.framework、QuartzCore.framework、OpenGLES.framework 與 SystemConfiguration.framework。

四、在 AppDelegate 中初始化

此處需先到百度地圖 API 網站申請一個應用 key。AppDelegate.h 如下:

#import <UIKit/UIKit.h>

#import "TestViewController.h"

#import "BMapKit.h"

#define BaiduMapKey @"A56A733C30B159166B74AD41530CB013685035F9"

@interface AppDelegate : UIResponder <UIApplicationDelegate> {

    BMKMapManager* _mapManager;

}

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m 中則在啟動時建立並啟動 manager:

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

{

    _mapManager = [[BMKMapManager alloc] init];

    BOOL ret = [_mapManager start:BaiduMapKey generalDelegate:nil];

    if (!ret) {

        NSLog(@"BMKMapManager start failed!");

    }

  

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    self.window.backgroundColor = [UIColor whiteColor];

  

    TestViewController *root = [[TestViewController alloc] init];

    self.window.rootViewController = root;

  

    [self.window makeKeyAndVisible];

    return YES;

}

完成以上設定後,就能在專案中順利調用百度地圖的各項功能了。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved