日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS9定位获取经纬度 swift

發(fā)布時間:2023/12/31 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS9定位获取经纬度 swift 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2019獨角獸企業(yè)重金招聘Python工程師標準>>>

定位服務是很多程序中都用到的,主要使用CoreLocation庫中的CLLocationManager類和CLLocation類。

實現(xiàn)結(jié)果使用彈窗顯示,如下圖:

實現(xiàn)步驟:

首先,我們需要在Info.plist文件中添加兩個字段:

//始終開啟定位的用戶提示信息NSLocationAlwaysUsageDescription//僅在程序使用時開啟定位的用戶提示信息NSLocationWhenInUseUsageDescription

如下圖,Value為自定義信息,用來顯示在系統(tǒng)的設置界面中:

當程序打開時,如果用戶沒有選擇過是否允許App獲取定位信息,會彈出窗口顯示,在窗口中會顯示NSLocationWhenInUseUsageDescription的內(nèi)容:

在系統(tǒng)的設置-定位中,顯示如下(應用程序說明):

現(xiàn)在,可以開始寫代碼了:

import?UIKitimport?CoreLocationclass?ViewController:?UIViewController,CLLocationManagerDelegate?{var?locationManager:CLLocationManager!override?func?viewDidLoad()?{super.viewDidLoad()//如果設備沒有開啟定位服務if?!CLLocationManager.locationServicesEnabled(){dispatch_async(dispatch_get_main_queue()){SCMessageBox.showquick(self,?contentMsg:?"無法定位,因為您的設備沒有啟用定位服務,請到設置中啟用")}return}locationManager?=?CLLocationManager()//設置精確度locationManager.desiredAccuracy?=?kCLLocationAccuracyBest????????//變化距離??超過50米?重新定位locationManager.distanceFilter?=?50//在IOS8以上系統(tǒng)中,需要使用requestWhenInUseAuthorization方法才能彈窗讓用戶確認是否允許使用定位服務的窗口if?SCDevice.getVersion()?>=??8.0?{//狀態(tài)為,用戶還沒有做出選擇,那么就彈窗讓用戶選擇if?CLLocationManager.authorizationStatus()?==?CLAuthorizationStatus.NotDetermined?{locationManager.requestWhenInUseAuthorization()//locationManager.requestAlwaysAuthorization()}//狀態(tài)為,用戶在設置-定位中選擇了【永不】,就是不允許App使用定位服務else?if(CLLocationManager.authorizationStatus()?==?CLAuthorizationStatus.Denied){//需要把彈窗放在主線程才能強制顯示dispatch_async(dispatch_get_main_queue()){SCMessageBox.showquick(self,?contentMsg:?"無法定位,因為您沒有授權(quán)本程序使用定位,請至設置中開啟!")return}}}//設置定位獲取成功或者失敗后的代理,Class后面要加上CLLocationManagerDelegate協(xié)議locationManager.delegate?=?self????????//開始獲取定位信息,異步方式locationManager.startUpdatingLocation()}func?locationManager(manager:?CLLocationManager!,?didFailWithError?error:?NSError!)?{SCMessageBox.showquick(self,?contentMsg:?"定位發(fā)生異常:\(error)")}func?locationManager(manager:?CLLocationManager!,?didUpdateLocations?locations:?[AnyObject]!)?{if?locations.count?>?0{?//??使用last?獲取?最后一個最新的位置,?前面是上一次的位置信息var?locationInfo:CLLocation?=?locations.last?as!?CLLocationSCMessageBox.showquick(self,?contentMsg:?"經(jīng)度:\(locationInfo.coordinate.longitude),緯度:\(locationInfo.coordinate.latitude)")}}override?func?didReceiveMemoryWarning()?{super.didReceiveMemoryWarning()//?Dispose?of?any?resources?that?can?be?recreated.}}


轉(zhuǎn)載于:https://my.oschina.net/u/2276921/blog/527462

總結(jié)

以上是生活随笔為你收集整理的iOS9定位获取经纬度 swift的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。