前言

腾讯地图iOS SDK目前只提供了Objective-C版本的SDK, 因而如果是Swift我的项目, 则须要本人通过Bridging文件来将其引入

应用场景

Swift我的项目接入腾讯地图

接入流程

1、创立Swift我的项目, 自己采纳的是StoryBoard创立的我的项目, 不过应用办法是一样的:

2、将SDK的framework和bundle导入我的项目中:

3、创立HeaderFile, 通常明明为"项目名称-Bridging-header", 即:TencentMapSwiftDemo-Bridging-header.h, 放在根目录(地位放在那里都能够, 区别只是门路不同):

4、进入我的项目配置, 抉择TARGETS-TencentMapSwiftDemo, 而后进入到Build Setting中. 在搜寻栏中搜寻Bridging, 并在Objective-C Bridging Header选项中输出: $(SRCROOT)/TencentMapSwiftDemo-Bridging-header.h($(SRCROOT)为快捷指令, 能够间接辨认我的项目的根门路):

如果编译没有出错, 则进行第五步, 否则请查看门路是否正确, 是否有多余的空格/换行等等, 比方下列报错, 就是自己在输出的时候不小心在最初加了一个空格导致的门路谬误:

5、编译通过的话就能够在BridgingHeader文件中导入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 */

6、别忘了依据文档所示, 须要增加libsqlite3.tbd、libc++.tbd两个依赖库:

7、到此就能够应用地图SDK了, 首先在AppDelegate外面配置好Key:

import UIKit@mainclass 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    }}

8、最初, 附加一段ViewController中的根本应用:

import UIKitclass 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()                // 发动POI检索        searchCurrentPositionPois()    }}

图片示例:展现根本地图和坐标点左近POI的检索

作者:面糊

链接:https://www.jianshu.com/p/efa...

起源:简书

著作权归作者所有。商业转载请分割作者取得受权,非商业转载请注明出处。