gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:17:14 25 4
gpt4 key购买 nike

我尝试在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com