开发 iOS 地图SDK 开发指南 实用工具 基于 Swift 的地图应用开发

基于 Swift 的地图应用开发 最后更新时间: 2021年01月22日

本文档是基于SWift 2.2版本进行开发的。地图SDK为4.1.0,搜索功能为4.1.0 。

高德地图 iOS SDK 支持使用 Swift 语言进行LBS应用的开发,我们的官方Demo中也集成了Swift的示例,所有示例代码均已在Demo中实现。

下面进行简单流程介绍。

开始实践前别忘记申请Key,申请请参考:http://lbs.amap.com/api/ios-sdk/guide/create-project/get-key/

第一步,新建工程;

第二步,工程配置,请参考开发指南的创建工程章节。

第三步,SWift配置;

首先:新建桥接头文件(放在工程路径下),这里命名为 officialDemoSwift-Bridging-Header.h,在该头文件中import需要的库文件,代码如下:

#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <MAMapKit/MAMapKit.h>
#import "APIKey.h"

然后,左侧目录中选中工程名,在 TARGETS->Build Settings-> Swift Compiler - Code Generation -> Objective-C Bridging Header 中输入桥接文件的路径,如下图所示:

显示地图

以3D矢量地图SDK为例,进行介绍。

在 ViewController.swift 中,继承 MAMapViewDelegate 协议,在 viewDidLoad 方法中配置用户Key,初始化 MAMapView 对象,并添加到 Subview中。代码如下:

 

var mapView: MAMapView!
    var search: AMapSearchAPI!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.title = "Swift Demo"
        
        AMapServices.sharedServices().apiKey = APIKey
        initMapView()
        initSearch()
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        mapView.showsUserLocation = true
        mapView.userTrackingMode = MAUserTrackingMode.Follow
    }


func initMapView() {
        
        mapView = MAMapView(frame: self.view.bounds)
        mapView.delegate = self
        self.view.addSubview(mapView!)
    }
    
    func initSearch() {
        //        AMap
        search = AMapSearchAPI()
        search.delegate = self
    }

运行程序,地图显示出来了,就是这样简单~

逆地理编码查询

逆地理编码是高德提供的众多服务中的一种,被广泛用于iOS的LBS应用。实现逆地理编码查询功能需要搜索库(AMapSearchKit.framework)。

搜索模块提供的功能的实现步骤有以下四点:

(1) 初始化主搜索对象AMapSearchAPI,并继承搜索协议 AMapSearchDelegate 。

(2) 构造 Request 对象,配置搜索参数。

(3) 通过主搜索对象以 Request 对象为参数,发起搜索。

(4) 实现搜索协议中对应的回调函数,通过解析 Response 对象获取搜索结果。

这里通过定位获取当前位置的经纬度,在点击定位标注(小蓝点)时,进行逆地理编码,在弹出的气泡中显示定位点的地址。实现该场景有以下几个步骤:

1.开启定位,显示定位标注(小蓝点)。

2.在定位的回调函数中获取定位点的经纬度。

3.点击定位标注,执行逆地理编码查询。

4.在逆地理编码回调中设置定位标注的title和subtitle。

完整的代码如下:

 
// 发起逆地理编码请求
func searchReGeocodeWithCoordinate(coordinate: CLLocationCoordinate2D!) {
        let regeo: AMapReGeocodeSearchRequest = AMapReGeocodeSearchRequest()
        regeo.location = AMapGeoPoint.locationWithLatitude(CGFloat(coordinate.latitude), longitude: CGFloat(coordinate.longitude))
        self.search!.AMapReGoecodeSearch(regeo)
    }
    
    //MARK:- MAMapViewDelegate
    func mapView(mapView: MAMapView!, didLongPressedAtCoordinate coordinate: CLLocationCoordinate2D) {
        // 长按地图触发回调,在长按点进行逆地理编码查询
        searchReGeocodeWithCoordinate(coordinate)
    }

   //MARK:- AMapSearchDelegate 
    func AMapSearchRequest(request: AnyObject!, didFailWithError error: NSError!) {
        print("request :\(request), error: \(error)")
    }

    // 逆地理查询回调
    func onReGeocodeSearchDone(request: AMapReGeocodeSearchRequest, response: AMapReGeocodeSearchResponse) {

        print("response :\(response.formattedDescription())")
        
        if (response.regeocode != nil) {
            let coordinate = CLLocationCoordinate2DMake(Double(request.location.latitude), Double(request.location.longitude))
            
            let annotation = MAPointAnnotation()
            annotation.coordinate = coordinate
            annotation.title = response.regeocode.formattedAddress
            annotation.subtitle = response.regeocode.addressComponent.province
            mapView!.addAnnotation(annotation)
        }
    }

上面介绍了用Swift语言,基于高德地图 iOS SDK 开发的一个简单的LBS应用的方法,如果你觉得Swift够简单够直接,Just Do~

Swift的例子:https://github.com/hadesh/MyRoute

目前2d、3d demo中都已包含swift demo,如3d demo,选择target MAMapKit-3D-Demo-swift,run,就可以看到swift demo的效果了。

返回顶部 示例中心 常见问题 智能客服 公众号
二维码