Swift工程导入百度地图SDK(手动方式)

步骤基本分为两个部分:1.百度的步骤的注意点 2.为了应用能跑实际上要额外添加的步骤。

  1. 按照百度的手动流程走一遍。其中为了方便,将所有要的全部添加了。(图片是按照官方的流程手动导入百度地图sdk的注意点)Linked Frameworks and Libraries添加了的内容


    Swift工程导入百度地图SDK(手动方式)_第1张图片
    Linked Frameworks and Libraries添加了的内容

    方法:选中工程名,在右键菜单中选择Add Files to “工程名”…,从BaiduMapAPI_Map.framework||Resources文件中选择mapapi.bundle文件,并勾选“Copy items if needed”复选框,单击“Add”按钮,将资源文件添加到工程中。

    Swift工程导入百度地图SDK(手动方式)_第2张图片
    Other Linker Flags的定制
  2. 在百度开发平台上面申请到key。填入自己项目的Bundle Identifier,还有自己的项目名。获得安全码。


    Bundle key的查看。在项目的General那项那里就能看得到
  3. 新建一个.m文件之后,同意Xcode添加一个"工程名-Bridging-Header.h"的文件。将这个新建好的.m文件改为.mm文件。(其实这一步应该可以算作是百度官方手动导入流程的一部分,那时候官方流程有提到)
  4. 在"工程名-Bridging-Header.h"文件中添加要用到的百度地图的头文件
// 为了方便直接全部都放进去了。 
#import 
#import 
#import 
#import 
#import 
#import 
#import 
  1. 在appdelegate中直接初始化自己的MapManager,修改的代码如下:
    var _mapManager:BMKMapManager?
    var navigationController:UINavigationController?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        _mapManager = BMKMapManager()
        let ret:Bool = (_mapManager?.start("你自己软件获取到的安全码", generalDelegate: nil))!
        if (!ret){
            print("manager start failed!")
        }
        // Override point for customization after application launch.
        return true
    }
  1. 在info.plist中添加App Transport Security Settings,再在其中添加Allow的那两个项,他们两个的值修改为YES来使得可以连接网络。
    同时呢,还要在info.plist中添加Bundle display name,这个项填入你自己的项目名字。

  2. 在ViewController这边写代码了可以(这里的代码仅仅是生成一个全屏幕的百度地图视图)

import UIKit
class ViewController: UIViewController {
    var mapView:BMKMapView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        mapView = BMKMapView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
        self.view.addSubview(mapView!)
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
最终运行效果
  • PodFile方式
    直接按照百度的流程改好即可。

你可能感兴趣的:(Swift工程导入百度地图SDK(手动方式))