布景

在iOS15版别下,运用final class并承继BMKMapViewDelegate类是可以直接将UIKit集成至SwiftUI的,但是在iOS16及以上版别,该方法失效,具体表现为程序运行反常。

 final class BMView: UIViewController, BMKMapViewDelegate {
    private var mapView: BMKMapView?
    override func viewDidLoad() {
        super.viewDidLoad()
        mapView = BMKMapView(frame: self.view.frame)
        mapView?.delegate = self
        mapView?.zoomLevel = zoomLevel ?? 17
        self.view.addSubview(mapView!);
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        mapView?.viewWillAppear()
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        mapView?.viewWillDisappear()
    }
}

因而需求换一种方法来集成百度地图,在这篇文章中,将会以协调器(Coordinator)来兼容UIKit,并承继百度地图组件。

集成进程

接下来将会从根底环境的装备,到组件封装来介绍整个集成进程。

百度开发者注册

进入lbsyun.baidu.com/,假如没有账号则注册;在【运用管理】-> 【我的运用】里面点击创立运用,在完成后,获取到列表上的【访问运用(AK)】的值。

装备根底环境

  1. 确保ruby版别在3.0以上,低于该版别,则需求晋级,在这里不做具体论述
  1. 装置cocoapods sudo gem install -n /usr/local/bin cocoapods
  1. 进入项目后,pod setup
  1. 等待装置完成

项目集成百度地图环境

添加百度地图依赖

在项目根目录下修改Podfile(不存在则创立)

platform :ios, '17.4' #手机的体系
target 'YourProjectTarget'  do
    pod 'BaiduMapKit', '6.6.1'
    # 可选组件
    # pod 'BaiduMapKit/Map', '6.6.1'    # 集成地图Map包
    # pod 'BaiduMapKit/Search', '6.6.1' # 集成地图Search包
    # pod 'BaiduMapKit/Utils', '6.6.1'  # 集成地图Utils包
end

执行装置 pod install

SwiftUI集成百度地图

在完成后,会在根目录下生成:.xcworkspace文件

创立桥头文件

在项目根目录下创立Configuration/BMK-Bridge-Header,将涉及到需求运用的百度地图头文件信息引进

#ifndef BMKSwift_Bridging_Header_h
#define BMKSwift_Bridging_Header_h
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引进base相关一切的头文件
#import <BaiduMapAPI_Map/BMKMapComponent.h>//引进地图功用一切的头文件
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//引进检索功用一切的头文件
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引进核算工具一切的头文件
#import <BaiduMapAPI_Map/BMKMapView.h>//只引进所需的单个头文件
#endif /* BMKSwift_Bridging_Header_h */

在 TARGETS->Build Settings->Swift Compiler -> General -> Objective-C Briding Header 中输入桥接文件的途径(该途径要和桥接头文件地点的目录一致),如下图所示:

SwiftUI集成百度地图

添加ATS支持

从项目根目录下 Pods -> BiaduMapKit -> BaiduMapKit -> thirdlibs 下,将libssl.alibcrypto.a两文件复制到项目根途径下 libs

在 TARGETS->Build Phases-> Link Binary With Libaries中点击“+”按钮,在弹出的窗口中点击“Add Other”按钮,选择libssl.a和libcrypto.a添加到工程中。

创立地图组件

import SwiftUI
struct RestaurantMap: UIViewControllerRepresentable {
    var zoomLevel: Float?
    var width: Int?
    var height: Int?
    func makeUIViewController(context: Context) -> UIViewController {
        return context.coordinator
    }
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
    }
    func makeCoordinator() -> Coordinator {
        Coordinator(zoomLevel: zoomLevel, width: width, height: height)
    }
    internal class Coordinator: UIViewController, BMKMapViewDelegate {
        private var zoomLevel: Float?
        private var width: Int?
        private var height: Int?
        private var mapView: BMKMapView?
        init(zoomLevel: Float? = nil, width: Int? = nil, height: Int? = nil) {
            super.init(nibName: nil, bundle: nil)
            self.zoomLevel = zoomLevel
            self.width = width
            self.height = height
        }
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        override func viewDidLoad() {
            super.viewDidLoad()
            mapView = BMKMapView(frame: CGRect(x: 0, y: 0, width: width ?? Int(self.view.frame.width), height: height ?? Int(self.view.frame.height)))
            mapView?.delegate = self
            mapView?.zoomLevel = zoomLevel ?? 17
            self.view.addSubview(mapView!);
        }
        override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            mapView?.viewWillAppear()
        }
        override func viewWillDisappear(_ animated: Bool) {
            super.viewWillDisappear(animated)
            mapView?.viewWillDisappear()
        }
    }
}