How to fix speed being stuck at -1.0 mph?

My speed is stuck at -1.0 mph.



override func viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

locationManager.desiredAccuracy = kCLLocationAccuracyBest

locationManager.requestWhenInUseAuthorization()

locationManager.startUpdatingLocation()

locationManager.delegate = self

locationManager.startUpdatingLocation()

}



}


extension ViewController : CLLocationManagerDelegate {

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let location = locations.last!

latitude.text = String(location.verticalAccuracy)

longitude.text = String(location.horizontalAccuracy)

speed.text = String(format: "%.1f", location.speed) + " mph"

altitude.text = String(location.altitude)

print("Locatio: \(location)")

}

Replies

Is this on real device or simulator ?


Note that negative value indicates an invalid speed


Could you show what you get for all the print logs ? Notably

print("Locatio: \(location)")


Points to check:

- did you call for

locationManager.requestWhenInUseAuthorization()

What Claude said. Also note that the speed property is in meters per second. So you’ll have to convert it if you want mph.

Ive been using my real device. An example of what it prints:


Locatio: <+42.26438398,-87.83985747> +/- 65.00m (speed -1.00 mps / course -1.00) @ 11/20/18, 10:01:32 AM Central Standard Time

Locatio: <+42.26438398,-87.83985747> +/- 65.00m (speed -1.00 mps / course -1.00) @ 11/20/18, 10:01:33 AM Central Standard Time

Locatio: <+42.26437973,-87.83986170> +/- 65.00m (speed -1.00 mps / course -1.00) @ 11/20/18, 10:01:33 AM Central Standard Time

Locatio: <+42.26438089,-87.83986104> +/- 65.00m (speed -1.00 mps / course -1.00) @ 11/20/18, 10:01:33 AM Central Standard Time

Locatio: <+42.26440047,-87.83982946> +/- 65.00m (speed -1.00 mps / course -1.00) @ 11/20/18, 10:01:38 AM Central Standard Time

Locatio: <+42.26438659,-87.83982626> +/- 65.00m (speed -1.00 mps / course -1.00) @ 11/20/18, 10:01:44 AM Central Standard Time

Locatio: <+42.26438981,-87.83982756> +/- 65.00m (speed -1.00 mps / course -1.00) @ 11/20/18, 10:01:51 AM Central Standard Time

Locatio: <+42.26439094,-87.83982830> +/- 65.00m (speed -1.00 mps / course -1.00) @ 11/20/18, 10:01:57 AM Central Standard Time

Locatio: <+42.26439386,-87.83982834> +/- 65.00m (speed -1.00 mps / course -1.00) @ 11/20/18, 10:02:03 AM Central Standard Time

In my experience, 65 m accuracy means that’s a Wi-Fi based location. So you don’t get anything except lat and lon (no heading, speed etc.). Does the device in question have a GPS? Wi-Fi only iPads do not, for example.

When i look at thoses coordinates on the map, looks like yoiu did not move far. 30 meters it seems.


With the precision of 65m, it is thus possible thjat speed remains undefined (hence -1)


Seems you check the position evry 5 seconds or so. If you run at 5 m/s, that's 25m which is not enough.



You should either:

- use device with GPS activated

- or run a bike at high speed (12 m/s, ie 25 mph) to be over 65m

- or lower the frequency of position acquisition (1 every 20 seconds should be OK)


And always move in the same direction, to have enough distance between 2 points.

Did you solve it ? How ?