在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. iOS让数组内对象执行同一方法

    // 让数组中的所有对象都执行removeFromSuperview方法 [self.answerView.subviews makeObjectsPerformSelector:@selector( ...

  2. cocos2dx 3.x(捕鱼达人炮台角度换算)

    // // GameScence.hpp // NotesDamo // // Created by apple on 16/10/23. // // #ifndef GameScence_hpp # ...

  3. Spring AOP 问与答

    AOP的实现有哪些 AOP常见的实现有: Spring AOP Aspectj Guice AOP Jboss AOP 等 AOP Alliance 是什么, 为什么Spring AOP, G UIC ...

  4. MVC 学习系列

    总是很难说清MVC的概念,即使读了源代码后(读的时候有些东西,理解起来还是有点吃力),也依然能难对整体的每一个具体的原理说的一清二楚.为了达到自己学习的目的,我把自己的学习路线写成文章,一边自己能对M ...

  5. leetcode95 Unique Binary Search Trees II

    题目: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. F ...

  6. JAVA-面向对象-多态

    多态 1.方法重载 2.方法重写 3.对象转型 4.抽象(可以定义类和方法)    (关键字  abstract)   ( 如: public abstract class robot  )(不能修饰 ...

  7. [转]有哪些值得关注的技术博客(Java篇)

    有哪些值得关注的技术博客(Java篇)   大部分程序员在自学的道路上不知道走了多少坑,这个视频那个网站搞得自己晕头转向.对我个人来说我平常在学习的过程中喜欢看一些教程式的博客.这些博客的特点: 1. ...

  8. LED流水灯(二)

    记住看汇编的时候是红在上面 黑色在下面 startup.s 程序 ; MDK跑马灯实验; PRESERVE8               // 字节对齐关键词 ,汇编有8位对齐的要求,要添加 AREA ...

  9. AS-demo09

    ,mainifast: <uses-permission android:name="android.permission.SET_WALLPAPER"/> , < ...

  10. mac ruby rails安装(使用rvm)

    mac的场合: which ruby -> /usr/bin/ruby -> 这是mac自带的ruby,我们希望能用管理ruby的版本. 安装rvm curl -L https://get ...