Swift access to Tencent location service map SDK

Preface

Tencent Maps iOS SDK currently only provides the Objective-C version of the SDK, so if it is a Swift project, you need to import it through the Bridging file yourself

scenes to be used

Swift project access to Tencent map

Access process

1. Create a Swift project, I used the project created by StoryBoard, but the method of use is the same:

2. Import the SDK framework and bundle into the project:

3. Create a HeaderFile, usually clearly named "Project Name-Bridging-header", that is: TencentMapSwiftDemo-Bridging-header.h, and put it in the root directory (the location can be placed there, the difference is just the path):

4. Enter the project configuration, select TARGETS-TencentMapSwiftDemo, and then enter the Build Setting. Search for Bridging in the search bar, and enter in the Objective-C Bridging Header option: it $(SRCROOT)/TencentMapSwiftDemo-Bridging-header.h($(SRCROOT)is a shortcut command that can directly identify the root path of the project):

If there is no error in the compilation, go to step 5. Otherwise, please check whether the path is correct, whether there are extra spaces/line breaks, etc., such as the following error, it is the path error caused by accidentally adding a space at the end when I input :

5. After the compilation is passed, the Objective-C framework can be imported into the BridgingHeader file:

#ifndef TencentMapSwiftDemo_Bridging_header_h
#define TencentMapSwiftDemo_Bridging_header_h

#import <QMapKit/QMapKit.h>
#import <QMapKit/QMSSearchKit.h>

#endif /* TencentMapSwiftDemo_Bridging_header_h */

6. Don't forget that according to the documentation, you need to add two dependent libraries: libsqlite3.tbd and libc++.tbd:

7. At this point, you can use the map SDK. First, configure the Key in AppDelegate:

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
    }
}

8. Finally, add a section of basic usage in 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()
        
        // 发起POI检索
        searchCurrentPositionPois()
    }
}

Picture example: show the basic map and the retrieval of POI near the coordinate point

Author: batter

Link: https://www.jianshu.com/p/efa24e1487f4

Source: Brief Book

The copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Guess you like

Origin blog.csdn.net/weixin_45628602/article/details/109515424