0

I am trying to display the distance between two locations to the user. They hit the start button, go to their destination and hit the stop button. The distance will then be displayed to a label. It is to calculate paddock widths so a straight line from point a to point b is required.

I can run the code but just get a crash.

Here is the code I have so far:

import UIKit

import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet var label: UILabel!

var locationManager = CLLocationManager()

var startLocation: CLLocation!
var endLocation: CLLocation!
var distanceTravelled = Double()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func startButton(_ sender: Any) {

    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.startUpdatingLocation()

    } else {

        // Get user to allow location under device settings.

    }

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

        if startLocation == nil {

            startLocation = locations[0] as CLLocation

        } else {

            // Reset the locations.

        }

    }

}

@IBAction func stopButton(_ sender: Any) {

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

        if endLocation == nil {

            endLocation = locations[0] as CLLocation

        } else {

            // Reset the locations.

        }

        self.locationManager.stopUpdatingLocation()

    }

    distanceTravelled = startLocation.distance(from: endLocation)

    label.text = "\(distanceTravelled) m"

}


}

Can anyone see why this isn't working?

Thanks, Josh

Paulw11
  • 108,386
  • 14
  • 159
  • 186
J.Bury
  • 21
  • 5
  • 1
    What are the details of the crash? Which line does it crash on? Your use of implicitly unwrapped optionals instead of optionals for your start and end location would be my suspicion. – Paulw11 Aug 19 '17 at 22:39
  • I get fatal error: unexpectedly found nil while unwrapping an Optional value 2017-08-20 10:42:55.720726+1200 locationManager[541:134556] fatal error: unexpectedly found nil while unwrapping an Optional value (lldb). It crashes when I hit the stop button. – J.Bury Aug 19 '17 at 22:43
  • Make sure you have connected the IBOutlet in your storyboard and change your location variables to optionals and unwrap them conditionally in your code. – Paulw11 Aug 19 '17 at 22:46
  • Thanks for your input. Have figured it out now, didn't need the didUpdateLocations function. – J.Bury Aug 20 '17 at 03:46

0 Answers0