在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];//开启定位
 
 
下面自己写的swift调用
 import CoreLocation

class xxxxx: UIViewController,CLLocationManagerDelegate{

 
  override func viewDidLoad()
{
  super.viewDidLoad()

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()

}
 
  func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        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传感器的调用的更多相关文章

  1. iOS 里面 Swift与Objective-C混编,Swift与C++混编的一些比较

        即使你尽量用Swift编写iOS程序,难免会遇到部分算法是用C++语言编写的.那你只能去问问”度娘“或“狗哥”怎么用Swift调用C++算法.   一,C,C++, Objective-C,S ...

  2. iOS开发Swift篇—(一)简单介绍

    iOS开发Swift篇—简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objective-C ...

  3. iOS开发Swift篇—(七)函数(1)

    iOS开发Swift篇—(七)函数 一.函数的定义 (1)函数的定义格式 func 函数名(形参列表) -> 返回值类型 { // 函数体... } (2)形参列表的格式 形参名1: 形参类型1 ...

  4. iOS开发Swift篇—(九)属性

    iOS开发Swift篇—(九)属性 一.类的定义 Swift与Objective-C定义类的区别 Objective-C:一般需要2个文件,1个.h声明文件和1个.m实现文件 Swift:只需要1个. ...

  5. iOS开发Swift篇—(十)方法

    iOS开发Swift篇—(十)方法 一.简单说明 跟其他面向对象语言一样,Swift中的方法可以分为2大类: (1)实例方法(Instance Methods) 在OC中,实例方法以减号(-)开头 ( ...

  6. iOS开发Swift篇—简单介绍

    iOS开发Swift篇—简单介绍 一.简介 Swift是苹果于2014年WWDC(苹果开发者大会)发布的全新编程语言 Swift在天朝译为“雨燕”,是它的LOGO 是一只燕子,跟Objective-C ...

  7. [ios][swift]swift混编

    http://blog.csdn.net/iflychenyang/article/details/8876542(如何在Objective-C的头文件引用C++的头文件) 1.将.m文件扩展名改为. ...

  8. 李洪强iOS开发Swift篇—10_方法

    李洪强iOS开发Swift篇—10_方法 一.简单说明 跟其他面向对象语言一样,Swift中的方法可以分为2大类: (1)实例方法(Instance Methods) 在OC中,实例方法以减号(-)开 ...

  9. 李洪强iOS开发Swift篇—09_属性

    李洪强iOS开发Swift篇—09_属性 一.类的定义 Swift与Objective-C定义类的区别 Objective-C:一般需要2个文件,1个.h声明文件和1个.m实现文件 Swift:只需要 ...

随机推荐

  1. ASP.NET在删除掉数据库文件后报错处理

    在开发asp.net mvc程序时,默认时我们会使用LocalDB, 我们有时会以为删除掉App_Data目录就可以自动新建数据库,但是我们在网站重新启动后(进入Account)就会发现报如下错误: ...

  2. Android Log图文详解

    android.util.Log常用的方法有以下5个:Log.v() Log.d() Log.i() Log.w() 以及 Log.e() .根据首字母对应VERBOSE,DEBUG,INFO, WA ...

  3. Leetcode: Number of Islands II && Summary of Union Find

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  4. SQL事物

    事务:保障流程的完整执行就像银行取钱,先在你账上扣钱,然后存入别人的账上:但是从你账上扣完钱了,突然网断了,对方没有收到钱,那么此时你的钱也没了,别人的钱也没加上,为了防止此类情况的出现,事务. be ...

  5. php-引号中出现$

    当双引号中包含变量时,变量对应的值会与双引号中的内容连接在一起: 当单引号中包含变量时,变量会被当做字符串输出. 慕课网,I love you!慕课网,$love

  6. jnlp jws

    http://www.mkyong.com/java/java-web-start-jnlp-tutorial-unofficial-guide/

  7. jni

    http://www.cnblogs.com/likwo/archive/2012/05/21/2512400.html

  8. JAVA测试装饰者模式

    package shb.java.demo; /** * 测试装饰者模式 * @package :shb.java.demoJava02 * @author shaobn * @Describe : ...

  9. iphone设置铃声

    iphone同步铃声 1.下载itunes 2.打开itunes.文件->将文件添加到资料库...选择一首歌曲加进去 3.右击新加的歌曲,显示简介->选项.调整结束开始时间.不得超过40秒 ...

  10. RMAN连接数据库

    连接本地数据库: 方法1: C:\Documents and Settings\Administrator>set oracle_sid=jssweb C:\Documents and Sett ...