0

There are a few Q/A's on this topic e.g. here and here but I'm trying to adapt these in Swift UI to calculate an array of distances between consecutive points during a run. The idea being that eventually I can get a list of 'distance travelled every 0.2 miles' or similar.

But what I want first is... distance between location 1 and location 2, location 2 and location 3, location 3 and location 4 etc - to calculate total distance travelled on a run.

To do this, I'm trying this code :

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    lastSeenLocation = locations.last
    fetchCountryAndCity(for: locations.last)
    print (locations)
    

This works fine and prints an updating list of locations to simulator but then the below is meant to get each location that is added, use that as the start point and the next one as the end point and for now simply print each distance to console. Then calculate the distance between them using the getDistance function below. This doesn't work with error "Type of expression is ambiguous without more context" on the let distance = ... line.

    var total: Double = 00.00
         for i in 0..<locations.count - 1 {
             let start = locations[i]
             let end = locations[i + 1]
            let distance = getDistance(from: start,to: end)
             total += distance
}
print (total)
            

This is my function for getting the actual distance between 2 points, which I've taken from one of the other questions/answers posted above.

func getDistance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> CLLocationDistance {
       let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
       let to = CLLocation(latitude: to.latitude, longitude: to.longitude)
       return from.distance(from: to)
}

Help greatly appreciated! I have tried to format my question and code carefully after some feedback from another question but please tell me if I'm doing something wrong or can make it easier!

nc14
  • 539
  • 1
  • 8
  • 26
  • What is the `locations` array you are using in the `for` loop? How is it declared? How do you update it in the `didUpdateLocations` delegate function? Where do you call this code with the `for` loop? Is it in the delegate function itself? If so then your `getDistance` function is expecting different parameters to what you are supplying (also, there is likley to be only one element in the `locations` array each time the delegate fires; you will need to,keep,a history in your own array – Paulw11 Apr 30 '21 at 22:39
  • sorry I'm not 100% certain what you mean (I'm new) but I think locations array is created here... `func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])` what parameters would be expected? – nc14 Apr 30 '21 at 22:45

1 Answers1

1

The reason you are getting the error about an ambiguous expression is because the arguments you are passing doesn't match the type of the arguments in your function. locations[i]is a CLLocation while you function wants CLLocationCoordinate2D.

Your function then creates CLLocations anyway, so you can just fix the parameter types for your function.

You have a bigger problem, however. You are relying on the locations array that is passed to the delegate function. Although this may contain multiple locations in the case where location updates were deferred for some reason, in practice it will only contain a single location update.

You will need to create your own locations array to keep the history for calculation purposes.

Paulw11
  • 108,386
  • 14
  • 159
  • 186