博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等)
阅读量:4306 次
发布时间:2019-06-06

本文共 2676 字,大约阅读时间需要 8 分钟。

CoreLocation是iOS中一个提供设备定位的框架。通过这个框架可以实现定位处理,从而获取位置数据,比如经度、纬度、海拔信息等。

1,定位精度的设置
定位服务管理类CLLocationManager的desiredAccuracy属性表示精准度,有如下6种选择:

kCLLocationAccuracyBestForNavigation :精度最高,一般用于导航

kCLLocationAccuracyBest : 精确度最佳
kCLLocationAccuracyNearestTenMeters :精确度10m以内
kCLLocationAccuracyHundredMeters :精确度100m以内
kCLLocationAccuracyKilometer :精确度1000m以内
kCLLocationAccuracyThreeKilometers :精确度3000m以内

2,位置管理器更新频率的设置
我们无法直接控制位置管理器更新的频率,但可使用位置管理器的distanceFilter属性(单位米)进行间接控制。
它指设备(水平或垂直)移动多少米后才将另一个更新发送给委托。定位要求的精度越高,distanceFilter属性的值越小,应用程序的耗电量就越大。
3,计算两个坐标间的距离
通过CCLocation对象的distanceTo方法,可以得到两个坐标间的距离,单位是米。
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,下面通过一个样例演示如何获取设备相关的位置数据(经度,纬度,海拔,速度等信息)    
 

(1)在 info.plist里加入定位描述(Value值为空也可以): 
NSLocationWhenInUseDescription :允许在前台获取GPS的描述 
NSLocationAlwaysUsageDescription :允许在后台获取GPS的描述 

(2)代码如下:

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)"
    
}
}

转载于:https://www.cnblogs.com/Free-Thinker/p/4843569.html

你可能感兴趣的文章
tomcat调优方案Maximum number of threads (200) created for connector with address null and port 8091...
查看>>
java类的加载机制
查看>>
MDK linker和debug的设置以及在RAM中调试
查看>>
CocosCreator2.1.0渲染流程与shader
查看>>
制作新网络框架快速自动生成消息结构体的编辑器
查看>>
[转]Device Context 设备环境 设备上下文 理解
查看>>
事务的传播性和隔离级别
查看>>
2018.3.24 struct
查看>>
Linux系统删掉多个文件
查看>>
【随笔】Win7下GVIM的安装与配置
查看>>
协程,IO模式
查看>>
移动端meta标签
查看>>
是前端类库还是前端框架?
查看>>
解决glib2.0缺失问题 分类: LINUX 20...
查看>>
一些杂想
查看>>
js原型和原型链
查看>>
工作区和暂存区
查看>>
cf B. Fixed Points
查看>>
4步win7下简单FTP服务器搭建(试验成功)
查看>>
C#特性-表达式树
查看>>