1
我試圖顯示用戶的平均速度, ,我也想顯示數組的最高值。陣列「CoreLocation」的平均速度和最高速度
我搜索了論壇,發現了很多方法來完成這個,但沒有任何工作。
我曾嘗試在// top speed
和// average speed
這裏是我的代碼:
// Location
let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span = MKCoordinateSpanMake(0.015, 0.015)
let myLocation = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region = MKCoordinateRegionMake(myLocation, span)
mapView.setRegion(region, animated: true)
self.mapView.showsUserLocation = true
// Altitude
let altitude = location.altitude
let altitudeNoDecimals = Int(altitude)
altitudeLabel.text = "\(altitudeNoDecimals)"
// m/s to km/h
let kmt = location.speed * (18/5)
let kmtLabel = Int(kmt)
statusLabel.text = "\(kmtLabel)"
// Top Speed
// let maxSpeed: Int = (kmtLabel as AnyObject).value(forKeyPath: "@maxSpeed.self") as! Int
// topSpeedLabel.text = "\(maxSpeed)"
let max = location.toIntMax()
topSpeedLabel.text = "\(max)"
// Average speed
var avg: Double = (list as AnyObject).valueForKeyPath("@avg.self") as Double
averageSpeed.text = "\(avg)"
}
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
爲什麼選擇投票,請評論。 –
感謝您在我的代碼和解釋性評論中努力實現您的答案。它像一個魅力一樣工作! –
很高興我能幫到你。 –