前言
騰訊地圖iOS SDK目前只提供了Objective-C版本的SDK, 因此如果是Swift項(xiàng)目, 則需要自己通過Bridging文件來將其引入
使用場景:
Swift項(xiàng)目接入騰訊地圖.
準(zhǔn)備:
接入流程
-
創(chuàng)建Swift項(xiàng)目, 本人采用的是StoryBoard創(chuàng)建的項(xiàng)目, 不過使用方法是一樣的:
image.png -
將SDK的framework和bundle導(dǎo)入項(xiàng)目中:
image.png -
創(chuàng)建HeaderFile, 通常明明為"項(xiàng)目名稱-Bridging-header", 即:TencentMapSwiftDemo-Bridging-header.h, 放在根目錄(位置放在那里都可以, 區(qū)別只是路徑不同):
image.png -
進(jìn)入項(xiàng)目配置, 選擇TARGETS-TencentMapSwiftDemo, 然后進(jìn)入到Build Setting中. 在搜索欄中搜索Bridging, 并在Objective-C Bridging Header選項(xiàng)中輸入:
$(SRCROOT)/TencentMapSwiftDemo-Bridging-header.h($(SRCROOT)為快捷指令, 可以直接識(shí)別項(xiàng)目的根路徑):
image.png
如果編譯沒有出錯(cuò), 則進(jìn)行第五步, 否則請(qǐng)檢查路徑是否正確, 是否有多余的空格/換行等等, 比如下列報(bào)錯(cuò), 就是本人在輸入的時(shí)候不小心在最后加了一個(gè)空格導(dǎo)致的路徑錯(cuò)誤:

-
編譯通過的話就可以在BridgingHeader文件中導(dǎo)入Objective-C的框架了:
#ifndef TencentMapSwiftDemo_Bridging_header_h #define TencentMapSwiftDemo_Bridging_header_h #import <QMapKit/QMapKit.h> #import <QMapKit/QMSSearchKit.h> #endif /* TencentMapSwiftDemo_Bridging_header_h */ -
別忘了根據(jù)文檔所示, 需要添加
libsqlite3.tbd、libc++.tbd兩個(gè)依賴庫:
image.png -
到此就可以使用地圖SDK了, 首先在AppDelegate里面配置好Key:
import UIKit @main class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { QMapServices.shared().apiKey = "我的Key" QMSSearchServices.shared()?.apiKey = "我的Key" return true } } -
最后, 附加一段ViewController中的基本使用:
import UIKit class ViewController: UIViewController, QMSSearchDelegate { var mapView : QMapView! lazy var tencentSearcher : QMSSearcher = { return QMSSearcher.init(delegate: self) }() // MARK: 配置MapView func setupMapView() { mapView = QMapView.init(frame: UIScreen.main.bounds) view.addSubview(mapView) } // MARK: 配置Searcher func searchCurrentPositionPois() { let currentCoordinate = mapView.centerCoordinate let searchOption = QMSPoiSearchOption() searchOption.keyword = "美食" searchOption.setBoundaryByNearbyWithCenter(currentCoordinate, radius: 1000, autoExtend: false) tencentSearcher.search(with: searchOption) } // MARK: Searcher 代理方法 func search(with poiSearchOption: QMSPoiSearchOption, didReceive poiSearchResult: QMSPoiSearchResult) { print(poiSearchResult) } func search(with searchOption: QMSSearchOption, didFailWithError error: Error) { print(error) } // MARK: 生命周期方法 override func viewDidLoad() { super.viewDidLoad() // 配置MapView setupMapView() // 發(fā)起POI檢索 searchCurrentPositionPois() } }
圖片示例:展示基本地圖和坐標(biāo)點(diǎn)附近POI的檢索(逼不得已才將log中的tel字段給馬賽克了, 別問我為什么, 問審核大哥)

點(diǎn)評(píng)
- 期待可以出Pods, 就不用手動(dòng)集成這么麻煩了
- 目前官方文檔中的示例只有Objective-C版本的, 期望可以出個(gè)Swfit版本




