我尝试在Swift 2中创建一个显示所有GPS信息的应用程序。 我可以恢复latitute,longitue,horizontalAccuracy,verticalAccuracy,海拔,距离和上限,但我无法恢复速度,而我到达Objective-C: - (
感谢您的帮助
以下是我的代码:
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) {
}
}
我测试了这段代码:
vitesseLabel.text = String(CLLocationSpeed())
但速度仍为0.00
答案 0 :(得分:4)
我终于找到了,所以这里是代码:
var speed: CLLocationSpeed = CLLocationSpeed()
speed = locationManager.location!.speed
vitesseLabel.text = String(format: "%.0f km/h", speed * 3.6)
答案 1 :(得分:1)
您不知道速度的原因是您没有在要求CLLocation为其speed
的地方编写任何代码。