[ios][swift]swift GPS传感器的调用
在Info.plist文件中添加如下配置:
(1)NSLocationAlwaysUsageDescription
(2)NSLocationWhenInUseUsageDescription
swift其实做法类似objectc:
locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=
10
;
if
(iOSVersion>=
8
) {
[locationManager requestWhenInUseAuthorization];
//使用程序其间允许访问位置数据(iOS8定位需要)
}
[locationManager startUpdatingLocation];
//开启定位
class xxxxx: UIViewController,CLLocationManagerDelegate{
m_lm = CLLocationManager()
m_lm.delegate = self
m_lm.desiredAccuracy = kCLLocationAccuracyBest
m_lm.distanceFilter = kCLLocationAccuracyKilometer
if #available(iOS 8.0, *) {
m_lm.requestAlwaysAuthorization()
m_lm.requestWhenInUseAuthorization()
}
m_lm.startUpdatingLocation()
print(locations)
}
import UIKit import CoreLocation class ViewController: UIViewController , CLLocationManagerDelegate { var locationManager : CLLocationManager!
var seenError : Bool = false
var locationFixAchieved : Bool = false
var locationStatus : NSString = "not Started" var info : UILabel? var longitudeLabel_int : UILabel? //the longitude before the degree sign (integer section)
var longitudeLabel_dec : UILabel? //the longitude after the degree sign (decimal section)
var latitudeLabel_int : UILabel? //the latitude before the degree sign (integer section)
var latitudeLabel_dec : UILabel? //the latitude after the degree sign (decimal section) var O_longitude : UILabel? //the degree sign
var O_latitude : UILabel? //the degree sign func initLocationManager() {
seenError = false
locationFixAchieved = false
locationManager = CLLocationManager() locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLLocationAccuracyKilometer locationManager.requestAlwaysAuthorization()
} override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib. let buttomEdge = UILabel(frame: CGRect(x: , y: , width: , height: 0.2))
buttomEdge.backgroundColor = UIColor.redColor() let signature = UILabel(frame: CGRect(x: , y: , width: , height: ))
signature.text = "孙毅 April 25th 2015"
signature.textColor = UIColor.whiteColor() info = UILabel(frame: CGRect(x: , y: , width: , height: ))
info!.textAlignment = NSTextAlignment.Center
info!.textColor = UIColor.whiteColor() longitudeLabel_int = UILabel(frame: CGRect(x: , y: , width: , height: ))
longitudeLabel_int!.textAlignment = NSTextAlignment.Right
longitudeLabel_int!.text = ""
longitudeLabel_int!.font = UIFont(name: "Times New Roman", size: )
longitudeLabel_int!.textColor = UIColor.blueColor() O_longitude = UILabel(frame: CGRect(x: , y: , width: , height: ))
O_longitude!.textAlignment = NSTextAlignment.Left
O_longitude!.text = "o"
O_longitude!.font = UIFont(name: "Arial", size: )
O_longitude!.textColor = UIColor.blueColor() longitudeLabel_dec = UILabel(frame: CGRect(x: , y: , width: , height: ))
longitudeLabel_dec!.textAlignment = NSTextAlignment.Left
longitudeLabel_dec!.text = ""
longitudeLabel_dec!.font = UIFont(name: "Times New Roman", size: )
longitudeLabel_dec!.textColor = UIColor.blueColor() latitudeLabel_int = UILabel(frame: CGRect(x: , y: , width: , height: ))
latitudeLabel_int!.textAlignment = NSTextAlignment.Right
latitudeLabel_int!.text = ""
latitudeLabel_int!.font = UIFont(name: "Times New Roman", size: )
latitudeLabel_int!.textColor = UIColor.redColor() O_latitude = UILabel(frame: CGRect(x: , y: , width: , height: ))
O_latitude!.textAlignment = NSTextAlignment.Left
O_latitude!.text = "o"
O_latitude!.font = UIFont(name: "Arial", size: )
O_latitude!.textColor = UIColor.redColor() latitudeLabel_dec = UILabel(frame: CGRect(x: , y: , width: , height: ))
latitudeLabel_dec!.textAlignment = NSTextAlignment.Left
latitudeLabel_dec!.text = ""
latitudeLabel_dec!.font = UIFont(name: "Times New Roman", size: )
latitudeLabel_dec!.textColor = UIColor.redColor() self.initLocationManager() self.view.addSubview(info!)
self.view.addSubview(longitudeLabel_int!)
self.view.addSubview(O_longitude!)
self.view.addSubview(longitudeLabel_dec!)
self.view.addSubview(latitudeLabel_int!)
self.view.addSubview(O_latitude!)
self.view.addSubview(latitudeLabel_dec!)
self.view.addSubview(buttomEdge)
self.view.addSubview(signature)
} func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
locationManager.stopUpdatingLocation()
if (nil != error) {
if (false == seenError) {
seenError = true
info!.text = "Error"
}
}
} func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { if (false == locationFixAchieved) {
locationFixAchieved = true
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as! CLLocation
var coordinate = locationObj.coordinate var degree : Int =
var minute : Int =
var second : Double = 0.0 degree = Int(coordinate.longitude)
minute = Int((coordinate.longitude - Double(degree)) * )
second = (coordinate.longitude - Double(degree) - Double(minute)/60.0) *
var second_4 = NSString(format: "%.4f", second) //show only 4 decimal places
if (degree >= ) {
longitudeLabel_int!.text = "\(degree)"
longitudeLabel_dec!.text = "\(minute)\' \(second_4.doubleValue)" E"
}
else {
longitudeLabel_int!.text = "\(-degree)"
longitudeLabel_dec!.text = "\(-minute)\' \(-second_4.doubleValue)" W"
} degree = Int(coordinate.latitude)
minute = Int((coordinate.latitude - Double(degree)) * )
second = (coordinate.latitude - Double(degree) - Double(minute)/60.0) *
second_4 = NSString(format: "%.4f", second)
if (degree >= ) {
latitudeLabel_int!.text = "\(degree)"
latitudeLabel_dec!.text = "\(minute)\' \(second_4.doubleValue)" N"
}
else {
latitudeLabel_int!.text = "\(-degree)"
latitudeLabel_dec!.text = "\(-minute)\' \(-second_4.doubleValue)" S"
}
}
} func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
var shouldIAllow = false switch status {
case CLAuthorizationStatus.Restricted:
locationStatus = "Restricted Access to location"
case CLAuthorizationStatus.Denied:
locationStatus = "User denied access to location"
case CLAuthorizationStatus.NotDetermined:
locationStatus = "Status not determined"
default:
locationStatus = "Allowed to location Access!"
shouldIAllow = true }
NSNotificationCenter.defaultCenter().postNotificationName("LabelHasBeenUpdated", object: nil)
if (true == shouldIAllow) {
info!.text = "Localization is allowed"
locationManager.startUpdatingLocation()
}
else {
info!.text = "Denied access: \(locationStatus)"
}
} override func didReceiveMemoryWarning() {
[ios][swift]swift GPS传感器的调用的更多相关文章
- iOS 里面 Swift与Objective-C混编,Swift与C++混编的一些比较
即使你尽量用Swift编写iOS程序,难免会遇到部分算法是用C++语言编写的.那你只能去问问”度娘“或“狗哥”怎么用Swift调用C++算法. 一,C,C++, Objective-C,S ...
- iOS开发Swift篇—(一)简单介绍
iOS开发Swift篇—简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objective-C ...
- iOS开发Swift篇—(七)函数(1)
iOS开发Swift篇—(七)函数 一.函数的定义 (1)函数的定义格式 func 函数名(形参列表) -> 返回值类型 { // 函数体... } (2)形参列表的格式 形参名1: 形参类型1 ...
- iOS开发Swift篇—(九)属性
iOS开发Swift篇—(九)属性 一.类的定义 Swift与Objective-C定义类的区别 Objective-C:一般需要2个文件,1个.h声明文件和1个.m实现文件 Swift:只需要1个. ...
- iOS开发Swift篇—(十)方法
iOS开发Swift篇—(十)方法 一.简单说明 跟其他面向对象语言一样,Swift中的方法可以分为2大类: (1)实例方法(Instance Methods) 在OC中,实例方法以减号(-)开头 ( ...
- iOS开发Swift篇—简单介绍
iOS开发Swift篇—简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objective-C ...
- [ios][swift]swift混编
http://blog.csdn.net/iflychenyang/article/details/8876542(如何在Objective-C的头文件引用C++的头文件) 1.将.m文件扩展名改为. ...
- 李洪强iOS开发Swift篇—10_方法
李洪强iOS开发Swift篇—10_方法 一.简单说明 跟其他面向对象语言一样,Swift中的方法可以分为2大类: (1)实例方法(Instance Methods) 在OC中,实例方法以减号(-)开 ...
- 李洪强iOS开发Swift篇—09_属性
李洪强iOS开发Swift篇—09_属性 一.类的定义 Swift与Objective-C定义类的区别 Objective-C:一般需要2个文件,1个.h声明文件和1个.m实现文件 Swift:只需要 ...
随机推荐
- Perl Debug error: SetConsoleMode failed, LastError=|6|
Windows Strawberry Perl. 解决办法: 1. 设置环境变量 TERM = dumb 2. 重启 CMD 参考资料: http://padre.perlide.org/trac/t ...
- iOS - (两个APP之间的跳转)
一个程序若要跳到另一个程序.需要在目标程序的plist文件里面修改: 打开info.plist,添加一项URL types 展开URL types,再展开Item0,将Item0下的URL ident ...
- Sqoop -- 用于Hadoop与关系数据库间数据导入导出工作的工具
Sqoop是一款开源的工具,主要用于在Hadoop相关存储(HDFS.Hive.HBase)与传统关系数据库(MySql.Oracle等)间进行数据传递工作.Sqoop最早是作为Hadoop的一个第三 ...
- iTOP-4412 平台基础软件的安装和学习
这两天在电脑上根据开发手册安装了超级终端.虚拟机.Ubuntu.以及Vim 编辑器等开发所必备的环境 1.笔记本没有串口,从实验室找了USB 转串口线来连接开发板和PC,从网盘找到并安装了USB 转串 ...
- PostgreSQL 一主两备节点(两备节点为同步节点)故障恢复
PostgreSQL 同步复制及故障恢复 10.2.208.10:node1:master 10.2.208.11:node2:standby1 同步 10.2.208.12:node3:stand ...
- 利用MyEclipes的反转工程来配置Hibernate各种配置
首先需要有设计好的数据库,然后创建一个Web Project然后右键点击项目选择MyEclipse→add Hibernate Capabilities →→ →→,然后如果没有管理员的话需要在选择M ...
- spark-submit常用参数
yarn模式默认启动2个executor,无论你有多少的worker节点 standalone模式每个worker一个executor,无法修改executor的数量 partition是RDD中的一 ...
- Oracle存储过程总结
1.存储过程结构 1.1 第一个存储过程 create or replace procedure proc1( para1 varchar2, para2 out varchar2, para3 in ...
- 夺命雷公狗---Thinkphp----9之中间层的创建,防止跨目录访问
我们创建一个CommonController.class.php的中间层,让后让别的控制器都直接继承CommonController这个控制器即可决解跨目录访问的问题, <?php namesp ...
- [CrunchBang]禁止“桌面上鼠标滚轮切换工作区桌面“
鼠标滚轮切换虚拟桌面相关问题, 编辑 ~/.config/openbox/rc.xml 在 <context name="Desktop">段: <mouse ...