Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等)
CoreLocation是iOS中一個(gè)提供設(shè)備定位的框架。通過這個(gè)框架可以實(shí)現(xiàn)定位處理,從而獲取位置數(shù)據(jù),比如經(jīng)度、緯度、海拔信息等。
1,定位精度的設(shè)置 定位服務(wù)管理類CLLocationManager的desiredAccuracy屬性表示精準(zhǔn)度,有如下6種選擇:kCLLocationAccuracyBestForNavigation?:精度最高,一般用于導(dǎo)航
kCLLocationAccuracyBest?: 精確度最佳
kCLLocationAccuracyNearestTenMeters?:精確度10m以內(nèi)
kCLLocationAccuracyHundredMeters?:精確度100m以內(nèi)
kCLLocationAccuracyKilometer?:精確度1000m以內(nèi)
kCLLocationAccuracyThreeKilometers?:精確度3000m以內(nèi)
?
var currentLocation = CLLocation(latitude: 52.104526, longitude: 51.111151) var targetLocation = CLLocation(latitude: 52.105526, longitude: 51.141151) var distance:CLLocationDistance = currentLocation.distanceFromLocation(targetLocation) println("兩點(diǎn)間距離是:\(distance)")4,下面通過一個(gè)樣例演示如何獲取設(shè)備相關(guān)的位置數(shù)據(jù)(經(jīng)度,緯度,海拔,速度等信息)?
??? (1)在 info.plist里加入定位描述(Value值為空也可以):? NSLocationWhenInUseDescription?:允許在前臺(tái)獲取GPS的描述? NSLocationAlwaysUsageDescription?:允許在后臺(tái)獲取GPS的描述?
(2)代碼如下: import UIKit import CoreLocationclass ViewController: UIViewController, CLLocationManagerDelegate {//定位管理器let locationManager:CLLocationManager = CLLocationManager()@IBOutlet weak var label1: UILabel!@IBOutlet weak var label2: UILabel!@IBOutlet weak var label3: UILabel!@IBOutlet weak var label4: UILabel!@IBOutlet weak var label5: UILabel!@IBOutlet weak var label6: UILabel!@IBOutlet weak var label7: UILabel!override func viewDidLoad() {super.viewDidLoad()//設(shè)置定位服務(wù)管理器代理locationManager.delegate = self//設(shè)置定位進(jìn)度locationManager.desiredAccuracy = kCLLocationAccuracyBest//更新距離locationManager.distanceFilter = 100發(fā)送授權(quán)申請(qǐng)locationManager.requestAlwaysAuthorization()if (CLLocationManager.locationServicesEnabled()){//允許使用定位服務(wù)的話,開啟定位服務(wù)更新locationManager.startUpdatingLocation()print("定位開始")}}//定位改變執(zhí)行,可以得到新位置、舊位置func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {//獲取最新的坐標(biāo)let currLocation:CLLocation = locations.last!label1.text = "經(jīng)度:\(currLocation.coordinate.longitude)"//獲取緯度label2.text = "緯度:\(currLocation.coordinate.latitude)"//獲取海拔label3.text = "海拔:\(currLocation.altitude)"//獲取水平精度label4.text = "水平精度:\(currLocation.horizontalAccuracy)"//獲取垂直精度label5.text = "垂直精度:\(currLocation.verticalAccuracy)"//獲取方向label6.text = "方向:\(currLocation.course)"//獲取速度label7.text = "速度:\(currLocation.speed)"} }
原文出自:www.hangge.com??轉(zhuǎn)載保留原文鏈接:http://www.hangge.com/blog/cache/detail_783.html
轉(zhuǎn)載于:https://www.cnblogs.com/LiuLady12138/p/5048521.html
總結(jié)
以上是生活随笔為你收集整理的Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SecutrCRTt 连接Virtual
- 下一篇: SQL中JOIN 的用法