mike vorisis mike vorisis - 1 year ago 62
Swift Question

Find the distance in Km with Swift

I want to calculate the distance from my location to another.
I found this line of code:

let distanceInMeters = location1.distanceFromLocation(location2)

But my problem is that I don't know how to take it from latitude and longitude.

Answer Source

So, you just need to create 2 CLLocation objects from your lat and long and then just call the line of code that you found.

let location1 = CLLocation(latitude: 20.0, longitude: 20.0)
let location2 = CLLocation(latitude: 30.0, longitude: 30.0)

let distanceInMeters = location1.distanceFromLocation(location2)

Also, this will return you distance in meters, not in km.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download