ios - 使用 CLLocation 获得 Swift 2 的速度

标签 ios performance swift2 cllocationmanager

我尝试在 Swift 2 中制作一个显示所有 GPS 信息的应用程序。 我可以恢复纬度、经度、水平精度、垂直精度、高度、距离和上限,但我无法恢复速度,而我到达了 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

最佳答案

我终于找到了,所以这是代码:

var speed: CLLocationSpeed = CLLocationSpeed()
speed = locationManager.location!.speed
vitesseLabel.text = String(format: "%.0f km/h", speed * 3.6)

关于ios - 使用 CLLocation 获得 Swift 2 的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33323427/

相关文章:

iOS 7 UIBarButtonItem 荒谬的间距问题

ios - Delphi/IOS 如何正确使用imp_implementationWithBlock?

ios - public private 和 protected in objective-c

java - 数独生成速度

ios - 前缀在 Xcode 7 beta 6 中是否正常工作?

ios - "Missing required 57x57 icon"适用于 PhoneGap Build 上的 iOS

optimization - postgres日常查询分析工具

performance - CUBLAS dgemm性能查询

ios - UIImage 类型的值必须为 bindTo

swift - 我应该为此选项添加什么标识符?