0

I am trying to calculate distance moved from a start point to an endpoint. My implemetation works but does not give accurate distance because it only calculates distance between a static start CLLocation to the pont of stop

here is my code

    enum DistanceValue: Int {
            case meters, miles
        }

        func calculateDistanceBetweenLocations(_ firstLocation: CLLocation, secondLocation: CLLocation, valueType: DistanceValue) -> Double {
            var distance = 0.0
            let meters = firstLocation.distance(from: secondLocation)
            distance += meters
            switch valueType {
            case .meters:
                return distance
            case .miles:
                let miles = distance
                return miles
            }
        }

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if startLocation == nil {
            startLocation = locations.first
        } else if let location = locations.last {
            runDistance += lastLocation.distance(from: location)
            let calc = calculateDistanceBetweenLocations(lastLocation, secondLocation: location, valueType: .meters)

            print("TOTAL LOC 1 \(calc)")
            print("TOTAL LOC 2 \(runDistance)")
        }
        lastLocation = locations.last

    }

as shown in my print statements print("TOTAL LOC 1 \(calc)") print("TOTAL LOC 2 \(runDistance)") how can I make

calc the same with runDistance

here is what is printed in the console

TOTAL LOC 10.29331530774379
TOTAL LOC 2 10.29331530774379
TOTAL LOC 2.2655118031831587
TOTAL LOC 2 12.558827110926948
King
  • 1,885
  • 3
  • 27
  • 84

0 Answers0