How to find out distance between gps location coordinates in Swift?


A very simple way to calculate the distance between two given CLLocationCoordinate2D is to first convert them to MKMapPoint which represents two-dimensional point on map.

let point1 = MKMapPoint(clcoordinate1)
let point2 = MKMapPoint(clcoordinate2)

Now, we just use distance(to:) method to receive the distance in meters. Below is the code:

func distance(to b: MKMapPoint) -> CLLocationDistance

Returns the number of meters between two map points.
This distance reflects the actual distance between the two points on the surface of the globe, taking into account the curvature of the Earth.

let distance = point1.distance(to: point2)

You might also like: How to Send Local Video to iMessage in Swift

You might also like: AVAudioPlayer not playing any sound

You might also like: How to Get Substring With NSRange in Swift 5

You might also like: Adding two numbers in macOS x86-64 Assembly - Part 2

You might also like: UIToolbar for keyboard inputAccessoryView gives Layout Constraint Errors

More Articles

Recommended posts