CoreLocation是iOS中一個提供設備定位的框架。經過這個框架能夠實現定位處理,從而獲取位置數據,好比經度、緯度、海拔信息等。git
kCLLocationAccuracyBestForNavigation :精度最高,通常用於導航
kCLLocationAccuracyBest : 精確度最佳
kCLLocationAccuracyNearestTenMeters :精確度10m之內
kCLLocationAccuracyHundredMeters :精確度100m之內
kCLLocationAccuracyKilometer :精確度1000m之內
kCLLocationAccuracyThreeKilometers :精確度3000m之內
swift
1
2
3
4
|
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,下面經過一個樣例演示如何獲取設備相關的位置數據(經度,緯度,海拔,速度等信息)
框架
(2)代碼以下:ide
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import
UIKit
import
CoreLocation
class
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)"
}
}
|