Distance between two coordinates with CoreLocation

Viewed 11072

I need to calculate the distance (in meters and miles) between two coordinates given

How can I do that?

4 Answers

swift 3 func distance(from location: CLLocation) -> CLLocationDistance Description Returns the distance (measured in meters) from the receiver’s location to the specified location. This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth. The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations. Parameters
location The other location. Returns The distance (in meters) between the two locations. SDKs iOS 3.2+, macOS 10.6+, tvOS 9.0+, watchOS 2.0+ Declared In Core Location More Method Reference

e.g.

let distance = location.distance(from: CLLocation(latitude: 
CLLocationDegrees(oldLocationLat), longitude: 
CLLocationDegrees(oldLocationLng)))

swift 3

func distance(from location: CLLocation) -> CLLocationDistance Description 

Returns the distance (measured in meters)

e.g.

locations: [CLLocation]

let location: CLLocation = locations.last!

let distance = location.distance(from: CLLocation(latitude: CLLocationDegrees(oldLocationLat), longitude: CLLocationDegrees(oldLocationLng)))
Related