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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等)

發布時間:2025/4/14 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

CoreLocation是iOS中一個提供設備定位的框架。通過這個框架可以實現定位處理,從而獲取位置數據,比如經度、緯度、海拔信息等。

1,定位精度的設置 定位服務管理類CLLocationManager的desiredAccuracy屬性表示精準度,有如下6種選擇:

kCLLocationAccuracyBestForNavigation?:精度最高,一般用于導航
kCLLocationAccuracyBest?: 精確度最佳
kCLLocationAccuracyNearestTenMeters?:精確度10m以內
kCLLocationAccuracyHundredMeters?:精確度100m以內
kCLLocationAccuracyKilometer?:精確度1000m以內
kCLLocationAccuracyThreeKilometers?:精確度3000m以內

2,位置管理器更新頻率的設置 我們無法直接控制位置管理器更新的頻率,但可使用位置管理器的distanceFilter屬性(單位米)進行間接控制。 它指設備(水平或垂直)移動多少米后才將另一個更新發送給委托。定位要求的精度越高,distanceFilter屬性的值越小,應用程序的耗電量就越大。 3,計算兩個坐標間的距離 通過CCLocation對象的distanceTo方法,可以得到兩個坐標間的距離,單位是米。

?

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("兩點間距離是:\(distance)")

4,下面通過一個樣例演示如何獲取設備相關的位置數據(經度,緯度,海拔,速度等信息)?

??
? (1)在 info.plist里加入定位描述(Value值為空也可以):? NSLocationWhenInUseDescription?:允許在前臺獲取GPS的描述? NSLocationAlwaysUsageDescription?:允許在后臺獲取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()//設置定位服務管理器代理locationManager.delegate = self//設置定位進度locationManager.desiredAccuracy = kCLLocationAccuracyBest//更新距離locationManager.distanceFilter = 100發送授權申請locationManager.requestAlwaysAuthorization()if (CLLocationManager.locationServicesEnabled()){//允許使用定位服務的話,開啟定位服務更新locationManager.startUpdatingLocation()print("定位開始")}}//定位改變執行,可以得到新位置、舊位置func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {//獲取最新的坐標let currLocation:CLLocation = locations.last!label1.text = "經度:\(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??轉載保留原文鏈接:http://www.hangge.com/blog/cache/detail_783.html

轉載于:https://www.cnblogs.com/LiuLady12138/p/5048521.html

總結

以上是生活随笔為你收集整理的Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。