項目一開始,又快到凌晨了,我給大家來個重磅戲,提提神。本節(jié)我們來將百度地圖SDK使用cocoapods集成到我們項目中,并使用swift2.1進(jìn)行一個簡單的地圖展示。
新建一個項目,依然選擇Single View Applicaton,項目名和公司隨意,開發(fā)語言記得選擇swift:

QQ20151218-1.png

QQ20151218-2.png
ok,選擇一個路徑并create,我們的項目就算是創(chuàng)建成功了,

QQ20151218-3.png
因為大家應(yīng)該都有了前面cocoapods的基礎(chǔ),這里我就不過多的廢話了,如果有后面新來的同學(xué)的話,請移步如何在iOS&swfit中使用第三方庫(CocoaPods)。
在根目錄新建我們的Podfile文件,寫入百度地圖的地址:
platform :ios, '8.0'
use_frameworks!
pod 'BaiduMapKit'```
ok,cd到當(dāng)前目錄并執(zhí)行 ```pod install```,因為這里是第一次,所以是使用 ```pod install```,如果是安裝成功后后面需要更新的話,則使用```pod update```,好吧,我又廢話了。
himideMacBook-Pro:~ himi$ cd /Users/himi/Desktop/show/sctong
himideMacBook-Pro:sctong himi$ pod install
Updating local specs repositories
Analyzing dependencies
Downloading dependencies
Installing BaiduMapKit (2.9.1)
Generating Pods project
Integrating client project
[!] Please close any current Xcode sessions and use sctong.xcworkspace for this project from now on.
Sending stats
Pod installation complete! There is 1 dependency from the Podfile and 1 total
pod installed.
himideMacBook-Pro:sctong himi$
好的,集成成功,我們關(guān)掉當(dāng)前項目,換成sctong.xcworkspace進(jìn)入工程,以后都只打開.xcworkspace的來進(jìn)行開發(fā)。打開后我們發(fā)現(xiàn),工程里多了一些文件,百度地圖的sdk已經(jīng)集成進(jìn)項目中了。

因為百度地圖SDK是Objective-c的源代碼,這里我們還需要創(chuàng)建頭文件并添加對其引用。創(chuàng)建一個C++ File,步驟如下:



最后一定要Create Bridging Header。OK,再把創(chuàng)建好的兩個mmmm文件刪除,留下自動創(chuàng)建的sctong-Bridging-Header.h文件,在其中添加對使用到的Objective-c源文件的import
import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相關(guān)所有的頭文件
import <BaiduMapAPI_Map/BMKMapComponent.h>//引入地圖功能所有的頭文件
import <BaiduMapAPI_Search/BMKSearchComponent.h>//引入檢索功能所有的頭文件
import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//引入云檢索功能所有的頭文件
import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的頭文件
import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//引入計算工具所有的頭文件
import <BaiduMapAPI_Radar/BMKRadarComponent.h>//引入周邊雷達(dá)功能所有的頭文件
import <BaiduMapAPI_Map/BMKMapView.h>//只引入所需的單個頭文件
保存關(guān)閉,現(xiàn)在我們需要到百度地圖開發(fā)者后臺去[創(chuàng)建一個應(yīng)用](http://lbsyun.baidu.com/apiconsole/key/create),如果你還沒有成為一個百度開發(fā)者,或者嫌麻煩的話,可以使用下面這個AK,**如果使用這個mmmm的話,需要把bundle identifier修改為com.mmmm**:
7516347 mmmm BRtrj48ZcxVzCuyLMiTQegr1 iOS端

現(xiàn)在我們集成好了SDK,并且有了AK密鑰,只需要最后的一步,就可以正式的開始上手使用地圖了,這一步就是修改我們的Info.plist,類似于android系統(tǒng)的AndroidMainfest.xml,我們需要到里面修改一下配置信息,申請一下定位權(quán)限:

直接在末尾加入下面代碼:

<key>CFBundleDisplayName</key>
<string>搜材通</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
OK,大功告成,到現(xiàn)在為止我們雖然做了很多工作,但卻什么效果都沒看到,是不是有點心急???現(xiàn)在我們來到AppDelegate中didFinishLaunchingWithOptions方法,
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let mapManager = BMKMapManager()
mapManager.start("換成你的AK密鑰", generalDelegate: nil)
return true
}
這樣我們start成功后就可以去ViewController中調(diào)用了,我們新建一個ViewController,代碼很少,我就沒寫注釋了:

class MapController: UIViewController,BMKMapViewDelegate,BMKLocationServiceDelegate {
var mapView:BMKMapView!
var locationService:BMKLocationService!
override func viewDidLoad() {
super.viewDidLoad()
mapView = BMKMapView(frame: self.view.frame)
mapView.showsUserLocation = true
//地圖縮放級別
mapView.zoomLevel=16;
locationService = BMKLocationService()
locationService.delegate = self
locationService.startUserLocationService()
self.view = mapView
}
func didUpdateUserHeading(userLocation: BMKUserLocation!) {
mapView.updateLocationData(userLocation)
}
func didUpdateBMKUserLocation(userLocation: BMKUserLocation!) {
mapView.updateLocationData(userLocation)
mapView.setCenterCoordinate(userLocation.location.coordinate, animated: true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
mapView?.delegate = self
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
mapView.showsUserLocation = false
mapView?.delegate = nil
}
}
現(xiàn)在我們修改didFinishLaunchingWithOptions中的rootViewController
self.window = UIWindow()
self.window!.frame = UIScreen.mainScreen().bounds
self.window!.rootViewController = MapController()
self.window!.makeKeyAndVisible()
OK,拿真機(jī)來跑一下

在用戶沒有操作前是默認(rèn)定位到首都的,點擊允許:

完美的把我的位置給暴露了,OK,這節(jié)我們就先到這里,本來是要提神的,結(jié)果一下寫了這么久。
Git地址:https://github.com/bxcx/sctong
本節(jié)分支:https://github.com/bxcx/sctong/tree/1st_BaiduMap