Swift 使用CoreLocation獲取定位與位置信息

首先是真機(jī)調(diào)試,模擬器不行,其次請?zhí)砑觾蓚€位置獲取權(quán)限Privacy - Location Always Usage Description和Privacy - Location When In Use Usage Description

添加兩個組件MapKit和CoreLocation

import?UIKit

import?MapKit

import?CoreLocation

class?FirstViewController2:UIViewController?,CLLocationManagerDelegate{


?var?locationManager :CLLocationManager!

?var?currLocation :CLLocation!


?lazyvar?mapView:MKMapView?= {

?let?mapView =MKMapView(frame:UIScreen.main.bounds)

?//用戶位置追蹤(用戶位置追蹤用于標(biāo)記用戶當(dāng)前位置,此時會調(diào)用定位服務(wù))

? ? ? ? mapView.userTrackingMode?= .followWithHeading

?//地圖的顯示風(fēng)格贯涎,此處設(shè)置使用標(biāo)準(zhǔn)地圖

? ? ? ? mapView.mapType?= .standard

?//地圖是否可滾動,默認(rèn)為true

? ? ? ? mapView.isScrollEnabled?=true

?//地圖是否縮放,默認(rèn)為true

? ? ? ? mapView.isZoomEnabled?=true

?//是否顯示用戶當(dāng)前位置?ios8之后才有荧止,默認(rèn)為false

? ? ? ? mapView.showsUserLocation?=true

?//為MKMapView設(shè)置delegate

?//mapView.delegate = self


?if#available(iOS9.0, *) {

? ? ? ? ? ? mapView.showsCompass?=true//顯示指南針

? ? ? ? ? ? mapView.showsScale?=true//顯示比例尺

? ? ? ? ? ? mapView.showsTraffic?=true//顯示交通

? ? ? ? }

? ? ? ? mapView.showsBuildings?=true

? ? ? ? mapView.showsUserLocation?=true


?return?mapView

? ? }()


?overridefunc?viewDidLoad() {

?view.backgroundColor?=UIColor.white

?//初始化位置管理器

?locationManager?=CLLocationManager()

?locationManager.delegate?=self

?//設(shè)備使用電池供電時最高的精度

?locationManager.desiredAccuracy?=kCLLocationAccuracyBest

?//精確到1000米,距離過濾器,定義了設(shè)備移動后獲得位置信息的最小距離

?locationManager.distanceFilter?=kCLLocationAccuracyKilometer

?if#available(iOS8.0, *) {

?//如果是IOS8及以上版本需調(diào)用這個方法

?locationManager.requestAlwaysAuthorization()

? ? ? ? ? ?//使用應(yīng)用程序期間允許訪問位置數(shù)據(jù)

?locationManager.requestWhenInUseAuthorization();

?//啟動定位

?locationManager.startUpdatingLocation()

? ? ? ? }

?view.addSubview(self.mapView)

? ? }

?//FIXME: CoreLocationManagerDelegate中獲取到位置信息的處理函數(shù)

?func?locationManager(_?manager:CLLocationManager, didUpdateLocations locations: [CLLocation]) {

?let?location:CLLocation?= locations[locations.count-1]asCLLocation

?currLocation?= location

?if?(location.horizontalAccuracy?>0) {

?self.locationManager.stopUpdatingLocation()

?//print("wgs84坐標(biāo)系?緯度: \(location.coordinate.latitude)經(jīng)度: \(location.coordinate.longitude)")

?self.locationManager.stopUpdatingLocation()

?//print("結(jié)束定位")

? ? ? ? }

?//坐標(biāo)轉(zhuǎn)換成地址

?let?geocoder =CLGeocoder()

? ? ? ? geocoder.reverseGeocodeLocation(currLocation) { (placemark, error) ->?Voidin

?if(error ==nil)//成功

? ? ? ? ? ? {

?let?array = placemark!asNSArray

?let?mark = array.firstObjectas!CLPlacemark


?let?FormattedAddressLines:NSString?= ((mark.addressDictionary!asNSDictionary).value(forKey:"FormattedAddressLines")asAnyObject).firstObjectas!NSString


?print(FormattedAddressLines)

}?else?{

?print(error!)//獲取位置信息失敗

? ? ? ? ? ? }

? ? ? ? }


? ? }

?//FIXME:?獲取位置信息失敗

?func? locationManager(_?manager:CLLocationManager, didFailWithError error:Error) {

?print(error)

? ? }


?overridefunc?viewWillAppear(_?animated:Bool) {

?super.viewWillAppear(animated)

?//創(chuàng)建左邊按鈕

?let?leftBtn =UIBarButtonItem(title:"<?返回", style: .plain, target:?self, action:#selector(chartViewController.btnBack(_:)))

?self.title?="定位地圖"

?self.navigationItem.leftBarButtonItem?= leftBtn

?self.navigationItem.leftBarButtonItem?.tintColor?=?UIColor(colorLiteralRed:255/255, green:255/255, blue:255/255, alpha:1)

?self.navigationController?.navigationBar.barTintColor?=

?UIColor(red:0/255, green:166/255, blue:124/255, alpha:1)

?self.navigationController?.navigationBar.titleTextAttributes?=

? ? ? ? ? ? [NSForegroundColorAttributeName:?UIColor(colorLiteralRed:255/255, green:255/255, blue:255/255, alpha:1)]

?//創(chuàng)建左邊保存按鈕

?let?item=UIBarButtonItem(title:"保存", style:UIBarButtonItemStyle.plain, target:self, action:#selector(threeViewController.tapped2))

?self.navigationItem.rightBarButtonItem?= item

?self.navigationItem.rightBarButtonItem?.tintColor?=?UIColor(colorLiteralRed:255/255, green:255/255, blue:255/255, alpha:1)

? ? }

?func?btnBack(_?sender:UIButton) {

?self.presentingViewController!.dismiss(animated:true, completion:nil)

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市跃巡,隨后出現(xiàn)的幾起案子危号,更是在濱河造成了極大的恐慌,老刑警劉巖素邪,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件外莲,死亡現(xiàn)場離奇詭異,居然都是意外死亡兔朦,警方通過查閱死者的電腦和手機(jī)偷线,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來沽甥,“玉大人淋昭,你說我怎么就攤上這事“步樱” “怎么了翔忽?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長盏檐。 經(jīng)常有香客問我歇式,道長,這世上最難降的妖魔是什么胡野? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任材失,我火速辦了婚禮,結(jié)果婚禮上硫豆,老公的妹妹穿的比我還像新娘龙巨。我一直安慰自己,他們只是感情好熊响,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布旨别。 她就那樣靜靜地躺著,像睡著了一般汗茄。 火紅的嫁衣襯著肌膚如雪秸弛。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天洪碳,我揣著相機(jī)與錄音递览,去河邊找鬼。 笑死瞳腌,一個胖子當(dāng)著我的面吹牛绞铃,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播嫂侍,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼儿捧,長吁一口氣:“原來是場噩夢啊……” “哼冷离!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起纯命,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤西剥,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后亿汞,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體瞭空,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年疗我,在試婚紗的時候發(fā)現(xiàn)自己被綠了咆畏。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡吴裤,死狀恐怖旧找,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情麦牺,我是刑警寧澤钮蛛,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站剖膳,受9級特大地震影響魏颓,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜吱晒,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一甸饱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧仑濒,春花似錦叹话、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至矗烛,卻和暖如春辅柴,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背瞭吃。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留涣旨,地道東北人歪架。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像霹陡,于是被迫代替她去往敵國和親和蚪。 傳聞我的和親對象是個殘疾皇子止状,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354

推薦閱讀更多精彩內(nèi)容