-14

I am trying to find the distance between a location that I am grabbing from a user and a location that I hard coded, but I am getting a error can't convert [CLLocation] to CLLocation.

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = CLLocationCoordinate2D(latitude: locations[0].coordinate.latitude, longitude: locations[0].coordinate.longitude)
     longitude = locations[0].coordinate.longitude
     latitude = locations[0].coordinate.latitude
    let coordinate₀ = CLLocation(latitude: 5.0, longitude: 5.0)

    let distanceInMeters = coordinate₀.distance(from: locations)
    print(distanceInMeters)
}

3 Answers3

1

It seems to me that you still don't understand the basics of the Swift language. Perhaps you should head over here and read up on the Swift documentation first.

You are using an array of CLLocation whereas the function distance(from: ) takes in an argument of a single CLLocation.

It looks like that you want to do is to replace your

let distanceInMeters = coordinate.distance(from: locations)

To let distanceInMeters = coordinate.distance(from: location)

Chan Jing Hong
  • 2,251
  • 4
  • 22
  • 41
0

You need to pass location instead of locations in the below line

let distanceInMeters = coordinate₀.distance(from: locations)
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
0

[CLLocation] is an array of CLLocation, try below

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = CLLocationCoordinate2D(latitude: locations[0].coordinate.latitude, longitude: locations[0].coordinate.longitude)
     let loc = locations[0]
     longitude = loc[0].coordinate.longitude
     latitude = loc[0].coordinate.latitude
    let coordinate₀ = CLLocation(latitude: 5.0, longitude: 5.0)

    let distanceInMeters = coordinate₀.distance(from: loc)
    print(distanceInMeters)
}
Yogesh Makwana
  • 448
  • 4
  • 16