Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等)
2016-04-06 10:12
507 查看
(1)在 info.plist里加入定位描述(Value值为空也可以):
NSLocationWhenInUseDescription :允许在前台获取GPS的描述
NSLocationAlwaysUsageDescription :允许在后台获取GPS的描述
(2)代码如下:
转自:http://www.hangge.com/blog/cache/detail_783.html#
NSLocationWhenInUseDescription :允许在前台获取GPS的描述
NSLocationAlwaysUsageDescription :允许在后台获取GPS的描述
(2)代码如下:
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)" } }
转自:http://www.hangge.com/blog/cache/detail_783.html#
相关文章推荐
- Apple Swift学习教程
- CSS浮动和定位学习笔记
- 介绍 Fedora 上的 Swift
- C#实现txt定位指定行完整实例
- 百度地图经纬度转换到腾讯地图/Google 对应的经纬度
- Flex中怎么给表格中的滚动条定位避免刷新回到原处
- 一个JavaScript操作元素定位元素的实例
- PHP根据两点间的经纬度计算距离
- C#通过经纬度计算2个点之间距离的实现代码
- js中的如何定位固定层的位置
- MYSQL 关于两个经纬度之间的距离由近及远排序
- JS弹出层单纯的绝对定位居中示例代码
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- python根据经纬度计算距离示例
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift编程中的泛型解析
- Swift中定义二维数组的方法及遍历方法示例
- 基于JavaScript实现高德地图和百度地图提取行政区边界经纬度坐标