[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:只需要 ...
随机推荐
- MVC 登录认证与授权及读取登录错误码
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 最近在自学MVC,遇到的问题很多,索性一点点总结下 ...
- 当As3遇见Swift(二)
字符串:String 都是用String来表示,都是值类型,在传递过程中都会进行拷贝. 计算字符数量 As3: str.length Swift: countElements(str) 数组:Arra ...
- @Transactional
转载请标明出处:http://blog.csdn.net/cuker919/archive/2010/10/21/5957209.aspx Spring事务的传播行为 在service类前加上@Tra ...
- http://codeforces.com/contest/555/problem/B
比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...
- nyist 62 笨小熊
http://acm.nyist.net/JudgeOnline/problem.php?pid=62 笨小熊 时间限制:2000 ms | 内存限制:65535 KB 难度:2 描述 笨小熊 ...
- Ruby操作Excel的方法与技巧大全
测试工作中,批量的数据通常会放到excel表格中,测试输出的数据写回表格中,这样输入输出易于管理,同时清晰明了 使用ruby来操作excel文件首先需要在脚本里包含以下语句 require'win32 ...
- C#: 自定义控件
(一)复合控件 http://wenku.baidu.com/link?url=y4BdtX3mOer4Hdin019jJpXJLi-2_ehmEo7i08cxEp1OR_3gb5CqaHrnNEB2 ...
- UML:时序图
时序图是用来描述对象的状态(或某数值)随时间变化而变化的图,一般软件开发中很少会用到. 灯有开和关两种状态,随着时间的推移,期间有人去开或者关这个灯,用时序图表示如下: 注意:蓝色和红色圈圈.黄色底色 ...
- mongo导出导入
导出例子: mongoexport -d test -c test -q '{sn:1}' -o test.dat 导入例子: mongoimport -d test -c students stud ...
- 类名.class, class.forName(), getClass()区别
1:Class cl=A.class; JVM将使用类A的类装载器, 将类A装入内存(前提是:类A还没有装入内存),不对类A做类的初始化工作.返回类A的Class的对象. 2:Class cl=对象引 ...