從頭開始swift2.1 仿搜材通項目(二) 百度地圖SDK集成

項目一開始,又快到凌晨了,我給大家來個重磅戲,提提神。本節(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)項目中了。
![QQ20151218-5.png](http://upload-images.jianshu.io/upload_images/1190844-91698ec3274c165d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
因為百度地圖SDK是Objective-c的源代碼,這里我們還需要創(chuàng)建頭文件并添加對其引用。創(chuàng)建一個C++ File,步驟如下:
![QQ20151218-6.png](http://upload-images.jianshu.io/upload_images/1190844-1c11c890ec90e787.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![QQ20151218-7.png](http://upload-images.jianshu.io/upload_images/1190844-fbe54bf9de60a24d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![QQ20151218-8.png](http://upload-images.jianshu.io/upload_images/1190844-266cbc9a34068605.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后一定要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端
![QQ20151218-9.png](http://upload-images.jianshu.io/upload_images/1190844-d8725b6b02ede885.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
現(xiàn)在我們集成好了SDK,并且有了AK密鑰,只需要最后的一步,就可以正式的開始上手使用地圖了,這一步就是修改我們的Info.plist,類似于android系統(tǒng)的AndroidMainfest.xml,我們需要到里面修改一下配置信息,申請一下定位權(quán)限:
![QQ20151219-1.png](http://upload-images.jianshu.io/upload_images/1190844-791ce5a148db84ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
直接在末尾加入下面代碼:
![QQ20151219-2.png](http://upload-images.jianshu.io/upload_images/1190844-14e3efb237f145e1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
<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,代碼很少,我就沒寫注釋了:
![QQ20151219-3.png](http://upload-images.jianshu.io/upload_images/1190844-023598ef25cad145.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

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ī)來跑一下
![Screen Shot 2015-12-19 at 00.21.28.png](http://upload-images.jianshu.io/upload_images/1190844-28ebfbc461537951.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在用戶沒有操作前是默認(rèn)定位到首都的,點擊允許:
![Screen Shot 2015-12-19 at 00.22.50.png](http://upload-images.jianshu.io/upload_images/1190844-8acb224688efc4b9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
完美的把我的位置給暴露了,OK,這節(jié)我們就先到這里,本來是要提神的,結(jié)果一下寫了這么久。
Git地址:https://github.com/bxcx/sctong
本節(jié)分支:https://github.com/bxcx/sctong/tree/1st_BaiduMap
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,405評論 4 61
  • 各位小伙伴們大家好,今天我向大家介紹一下蘋果百度地圖的使用方法,因為做過一些想關(guān)的APP,感覺百度地圖還是挺方便的...
    Lee0528閱讀 14,947評論 18 46
  • 本節(jié)是仿搜材通項目的最后一節(jié)了,前面我們記錄了主流框架(Tabbed)的搭建,第三方SDK(百度地圖)的集成,使用...
    a_mean閱讀 839評論 7 8
  • 最近難得清閑,閑來得空,記錄京城。 淺談京城歷史 內(nèi)外城------ 北京曾經(jīng)只是個無名小卒,但在...
    XINYI_ZH閱讀 1,337評論 1 4
  • 時代在進(jìn)步,科技在發(fā)展,越來越多的便利。很多人會說,這是時代的象征,科技實力的證明。這樣說其實沒什麼錯,不...
    韻杉成林閱讀 229評論 0 0

友情鏈接更多精彩內(nèi)容