[ios][swift]swift GPS传感器的调用

时间:2023-03-08 22:25:40

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