本文分為三部分,第一部分詳解用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
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”按鈕,將庫文件添加到工程中。
說明:
備注中,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 中輸入橋接文件的路徑
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!) } }
運行程序,地圖顯示出來了,就是這樣簡單~
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!, 21) mapView?.scaleOrigin = CGPointMake(scaleX!, 21) // 開啟定位 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 == 0){ 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
4、點擊獲取KEY按鈕。
第三部分 查看示例
---------------------------------------------------------------------------------------------------------------------------------
即日起至2016/10/31止,凡注冊成為高德開發者的新用戶,即可獲贈1張阿里雲優惠券,可享受最低6折購買阿里雲產品。數量有限,發完即止。詳情點擊:http://lbsbbs.amap.com/forum.php?mod=viewthread&tid=20143
---------------------------------------------------------------------------------------------------------------------------------