【iOS开发】如何用 Swift 语言进行LBS应用的开发?

时间:2024-03-26 00:06:26

本文分为三部分,第一部分详解用Swift语言开发LBS应用,并给出完整的示例与源代码;第二部分介绍如何申请LBS密钥,第三部分是综合示例查看,扫描二维码即可查看示例demo。

第一部分 使用Swift语言开发LBS应用

1、下载iOS地图SDK的最新版本,地址:http://lbs.amap.com/api/ios-sdk/down/

2、申请LBS密钥(见第二部分)。

3、xCode新建工程

新建一个 Single View Application 工程。注意:Language 选择 Swift

【iOS开发】如何用 Swift 语言进行LBS应用的开发?

4、工程配置

a.引入地图库&搜索库

左侧目录中选中工程名,在 TARGETS->Build Phases-> Link Binary With Libaries 中点击“+”按钮,在弹出的窗口中点击“Add Other”按钮,选择解压后的 MAMapKit.framework 文件添加到工程中。

搜索库的添加方法同上。

b.引入AMap.bundle资源文件

AMap.bundle资源文件中存储了定位、默认大头针标注视图等图片,可利用这些资源图片进行开发。

左侧目录中选中工程名,在右键菜单中选择Add Files to “工程名”…,从 MAMapKit.framework->Resources 文件夹中选择 AMap.bundle文件,并勾选“Copy items if needed”复选框,单击“Add”按钮,将资源文件添加到工程中。

c.引入系统库

左侧目录中选中工程名,在TARGETS->Build Settings-> Link Binary With Libaries中点击“+”按钮,在弹出的窗口中查找并选择所需的库(见下表),单击“Add”按钮,将库文件添加到工程中。

【iOS开发】如何用 Swift 语言进行LBS应用的开发?

说明:

备注中,2D表示使用2D栅格地图需要的系统文件,3D表示使用3D矢量地图需要的系统文件、Search表示使用搜索库需要的系统文件。

SystemConfiguration.framework、CoreTelephonySecurity.framework、Security.framework 是为了统计app信息使用。

d.Swift编译配置

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

#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>

然后,左侧目录中选中工程名,在 TARGETS->Build Phases-> Swift Compiler - Code Generation -> Objective-C Briding Header 中输入桥接文件的路径

【iOS开发】如何用 Swift 语言进行LBS应用的开发?

5、地图的显示

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

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

let APIKey = "8a1383b14466a8dbf362f44357c496c0"

class ViewController: UIViewController , MAMapViewDelegate{

    var mapView:MAMapView?

    override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// 配置用户Key
MAMapServices.sharedServices().apiKey = APIKey // 初始化MAMapView
initMapView()
} func initMapView(){ mapView = MAMapView(frame: self.view.bounds) mapView!.delegate = self self.view.addSubview(mapView!)
}
}

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

【iOS开发】如何用 Swift 语言进行LBS应用的开发?

6、一个实用的例子

逆地理编码为例,写一个完整的示例。实现步骤如下:

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

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

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

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

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

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

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

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

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

全部源代码:

import UIKit

let APIKey = "8a1383b14466a8dbf362f44357c496c0"

class ViewController: UIViewController ,MAMapViewDelegate, AMapSearchDelegate{

    var mapView:MAMapView?
var search:AMapSearchAPI?
var currentLocation:CLLocation? override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib. MAMapServices.sharedServices().apiKey = APIKey initMapView() initSearch()
} func initMapView(){ mapView = MAMapView(frame: self.view.bounds) mapView!.delegate = self self.view.addSubview(mapView!) let compassX = mapView?.compassOrigin.x let scaleX = mapView?.scaleOrigin.x //设置指南针和比例尺的位置
mapView?.compassOrigin = CGPointMake(compassX!, ) mapView?.scaleOrigin = CGPointMake(scaleX!, ) // 开启定位
mapView!.showsUserLocation = true // 设置跟随定位模式,将定位点设置成地图中心点
mapView!.userTrackingMode = MAUserTrackingModeFollow } // 初始化 AMapSearchAPI
func initSearch(){
search = AMapSearchAPI(searchKey: APIKey, delegate: self);
} // 逆地理编码
func reverseGeocoding(){ let coordinate = currentLocation?.coordinate // 构造 AMapReGeocodeSearchRequest 对象,配置查询参数(中心点坐标)
let regeo: AMapReGeocodeSearchRequest = AMapReGeocodeSearchRequest() regeo.location = AMapGeoPoint.locationWithLatitude(CGFloat(coordinate!.latitude), longitude: CGFloat(coordinate!.longitude)) println("regeo :\(regeo)") // 进行逆地理编码查询
self.search!.AMapReGoecodeSearch(regeo) } // 定位回调
func mapView(mapView: MAMapView!, didUpdateUserLocation userLocation: MAUserLocation!, updatingLocation: Bool) {
if updatingLocation {
currentLocation = userLocation.location
}
} // 点击Annoation回调
func mapView(mapView: MAMapView!, didSelectAnnotationView view: MAAnnotationView!) {
// 若点击的是定位标注,则执行逆地理编码
if view.annotation.isKindOfClass(MAUserLocation){
reverseGeocoding()
}
} // 逆地理编码回调
func onReGeocodeSearchDone(request: AMapReGeocodeSearchRequest!, response: AMapReGeocodeSearchResponse!) {
println("request :\(request)")
println("response :\(response)") if (response.regeocode != nil) { var title = response.regeocode.addressComponent.city var length: Int{
return countElements(title)
} if (length == ){
title = response.regeocode.addressComponent.province
}
//给定位标注的title和subtitle赋值,在气泡中显示定位点的地址信息
mapView?.userLocation.title = title
mapView?.userLocation.subtitle = response.regeocode.formattedAddress
} }
}

全部源码下载:https://github.com/hadesh/MyRoute

第二部分 如何申请LBS密钥

1、访问申请KEY地址:http://lbs.amap.com/console/key/

2、输入真实应用名称,选择iOS SDK平台服务。

3、获取Bundle Indentifier

获取方式一、代码获取

NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];

获取方式二、Xcode切换到General标签,查看Bundle Identifier

【iOS开发】如何用 Swift 语言进行LBS应用的开发?

4、点击获取KEY按钮。

【iOS开发】如何用 Swift 语言进行LBS应用的开发?

第三部分 查看示例

【iOS开发】如何用 Swift 语言进行LBS应用的开发?

---------------------------------------------------------------------------------------------------------------------------------

【iOS开发】如何用 Swift 语言进行LBS应用的开发?

即日起至2016/10/31止,凡注册成为高德开发者的新用户,即可获赠1张阿里云优惠券,可享受最低6折购买阿里云产品。数量有限,发完即止。详情点击:http://lbsbbs.amap.com/forum.php?mod=viewthread&tid=20143

---------------------------------------------------------------------------------------------------------------------------------