欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS学习:在工程中添加百度地图SDK

程序员文章站 2022-10-27 18:58:39
       1、将下载下来的sdk中的inc文件夹、mapapi.bundle、libbaidumapapi.a添加到工程中,...

 

     1、将下载下来的sdk中的inc文件夹、mapapi.bundle、libbaidumapapi.a添加到工程中,其中libbaiduapi.a有两个,一个对应模拟器一个对应真机,导入方法如下:


第一种方式:直接将对应平台的.a文件拖拽至xcode工程左侧的groups&files中,缺点是每次在真机和模拟器编译时都需要重新添加.a文件;

第二种方式:使用lipo命令将设备和模拟器的.a合并成一个通用的.a文件,将合并后的通用.a文件拖拽至工程中即可,具体命令如下:
lipo –create release-iphoneos/libbaidumapapi.a release-iphonesimulator/libbaidumapapi.a –output libbaidumapapi.a

第三种方式:

               1.将api的libs文件夹拷贝到您的application工程跟目录下

               2.在xcode的project -> edit active target -> build -> linking -> other linker flags中添加-objc

                3.设置静态库的链接路径,在xcode的project -> edit active target -> build -> search path -> library search paths中添加您的静态库目录,比                         如"$(srcroot)/../libs/release$(effective_platform_name)",$(srcroot)宏代表您的工程文件目录,$(effective_platform_name)宏代表当前配置是os还是simulator


我是用第二种方法,在真机和模拟器下都可以调试。


     2、因为静态库采用object c++实现,所以在工程中至少要有一个.mm的文件存在(可以把appdelegate.m改为.mm)


     3、导入工程所需的框架:corelocation.framework,quartzcore.framework,opengles.framework,systemconfiguration.framework


     4、在appdelegate中添加bmkmapmanager对象,这里要在百度地图api网站上申请一个应用key
appdelegate.h文件如下:


[cpp]
#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 

#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文件如下:


[cpp]
 (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions 

    _mapmanager = [[bmkmapmanageralloc] init]; 
    bool ret = [_mapmanagerstart:baidumapkey generaldelegate:nil]; 
    if (!ret) { 
        nslog(@"bmkmapmanager start failed!"); 
    } 
    
    self.window = [[uiwindowalloc] initwithframe:[[uiscreenmainscreen] bounds]]; 
    // override point for customization after application launch.  
    self.window.backgroundcolor = [uicolorwhitecolor]; 
    
    testviewcontroller *root = [[testviewcontrolleralloc] init]; 
    self.window.rootviewcontroller = root; 
    
    [self.windowmakekeyandvisible]; 
    returnyes; 

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
    _mapmanager = [[bmkmapmanageralloc] init];
    bool ret = [_mapmanagerstart:baidumapkey generaldelegate:nil];
    if (!ret) {
        nslog(@"bmkmapmanager start failed!");
    }
  
    self.window = [[uiwindowalloc] initwithframe:[[uiscreenmainscreen] bounds]];
    // override point for customization after application launch.
    self.window.backgroundcolor = [uicolorwhitecolor];
  
    testviewcontroller *root = [[testviewcontrolleralloc] init];
    self.window.rootviewcontroller = root;
  
    [self.windowmakekeyandvisible];
    returnyes;
}