[英]Get the speed by Swift 2 with CLLocation
I try to make an application in Swift 2 that displays all the GPS informations. 我尝试在Swift 2中制作一个显示所有GPS信息的应用程序。 I can recover latitute, longitue, horizontalAccuracy, verticalAccuracy, altitude, distance and cap but I am unable to recover speed, whereas I arrived in Objective-C :-(
我可以恢复纬度,经度,水平精度,垂直精度,高度,距离和上限,但是我无法恢复速度,而我到达了Objective-C :-(
Thanks for your help 谢谢你的帮助
Below is my code: 下面是我的代码:
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var running = false
var pause = false
var locationManager: CLLocationManager = CLLocationManager()
var startLocation: CLLocation!
@IBOutlet weak var latitudeLabel: UILabel!
@IBOutlet weak var longitudeLabel: UILabel!
@IBOutlet weak var horizontalAccuracyLabel: UILabel!
@IBOutlet weak var verticalAccuracyLabel: UILabel!
@IBOutlet weak var capLabel: UILabel!
@IBOutlet weak var altitudeLabel: UILabel!
@IBOutlet weak var partielLabel: UILabel!
@IBOutlet weak var distanceLabel: UILabel!
@IBOutlet weak var vitesseLabel: UILabel!
@IBOutlet weak var moyenneLabel: UILabel!
@IBOutlet weak var timerLabel: UILabel!
@IBOutlet weak var totalDistanceLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.startUpdatingHeading()
startLocation = nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func startDistance(sender: AnyObject) {
startLocation = nil
running = true
}
@IBAction func stopDistance(sender: AnyObject) {
running = false
}
@IBAction func resetDistance(sender: AnyObject) {
startLocation = nil
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let latestLocation: AnyObject = locations[locations.count - 1]
latitudeLabel.text = String(format: "%.4f",latestLocation.coordinate.latitude)
longitudeLabel.text = String(format: "%.4f",latestLocation.coordinate.longitude)
horizontalAccuracyLabel.text = String(format: "%.4f",latestLocation.horizontalAccuracy)
verticalAccuracyLabel.text = String(format: "%.4f",latestLocation.verticalAccuracy)
altitudeLabel.text = String(format: "%.4f",latestLocation.altitude)
if running == true {
if startLocation == nil {
startLocation = latestLocation as! CLLocation
}
let distanceBetween: CLLocationDistance =
latestLocation.distanceFromLocation(startLocation)
distanceLabel.text = String(format: "%.2f", distanceBetween)
}
}
func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading)
{
capLabel.text = String(format: "%.4f",newHeading.magneticHeading)
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
}
} }
I tested this code : 我测试了这段代码:
vitesseLabel.text = String(CLLocationSpeed())
but speed remains at 0.00 但速度保持在0.00
I finally found, so here's the code: 我终于找到了,所以这是代码:
var speed: CLLocationSpeed = CLLocationSpeed()
speed = locationManager.location!.speed
vitesseLabel.text = String(format: "%.0f km/h", speed * 3.6)
您不知道速度的原因是,您没有在向CLLocation询问其speed
地方编写任何代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.