Reputation: 71
I try to make an application in Swift 2 that displays all the GPS informations. I can recover latitute, longitue, horizontalAccuracy, verticalAccuracy, altitude, distance and cap but I am unable to recover speed, whereas I arrived in 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
Upvotes: 0
Views: 6409
Reputation: 71
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)
Upvotes: 4
Reputation: 534914
The reason you don't know the speed is that you have not written any code where you ask the CLLocation for its speed
.
Upvotes: 2