0

I'm not a programmer but have been playing around with a side project for fun. I'm using Xcode 10, Swift 5. I've pieced together a handful of things through youtube and SO but I've searched and experimented with this for three days and I'm running into dead ends here.

I am trying to determine the distance between a user's current location and a preset point (in this case the airport). I am able to find the distance between two hard-coded locations. I am also able to find and print the user's current location in the console. But when I try to combine those I am struggling, most often getting the error

'Value of type 'CLLocationCoordinate2D' has no member 'distance''

The code that working is and gives me the distance in meters I would like is:

lazy var phx = CLLocation(latitude: 33.409016, longitude: -111.805576)
lazy var distanceFromPhoenixToVegas = las.distance(from: phx)

And this will print out the current location coordinates:

func printCoordinates(){
     if let location = locationManager.location?.coordinate {
            print (location)
     }
}

If any one could offer guidance I would appreciate it.

Side note, there are a lot of similar questions on Stack Overflow. There are questions about getting a user's current location, and questions about getting distances between two points. I believe this is essentially what I am asking, but it was only answered by the original poster, and the answer seems not exactly correct to me. iOS & Swift: display distance from current location?

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
Brad C
  • 13
  • 5

1 Answers1

0

coordinate is of type CLLocationCoordinate2D You only need locationManager.location

if let location = locationManager.location {
   print(phx.distance(from: location))
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • I had a few additional errors when I added this. "Value of optional type 'CLLocation?' must be unwrapped to refer to member 'distance' of wrapped base type 'CLLocation'" was the first. `lazy var distanceToAirport = currentLocation!.distance(from: phx)` Adding the ! resolved that. I had an if statement later comparing the distance and that threw a `Binary operator '<' cannot be applied to operands of type 'CLLocationDistance?' (aka 'Optional') and 'Int'` error. I moved the currentLocation variable outside of the function and it resolved that error. Thank you SO MUCH. – Brad C Dec 11 '19 at 23:11