John Doe John Doe - 1 year ago 190
Swift Question

How to find out distance between coordinates?

I want to make it so that it will show the amount of distance between two CLLocation coordinates. Is there someway to do this without a complex math formula? If there isn't how would you do it with a formula?

Answer Source

CLLocation has a distanceFromLocation method so given two CLLocations:

CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];

or in Swift 3:

//: Playground - noun: a place where people can play

import CoreLocation

let coordinate₀ = CLLocation(latitude: 5.0, longitude: 5.0)
let coordinate₁ = CLLocation(latitude: 5.0, longitude: 3.0)

let distanceInMeters = coordinate₀.distance(from: coordinate₁) // result is in meters

you get here distance in meter so 1 miles = 1609 meter

if(distanceInMeters <= 1609)
 // under 1 mile
 // out of 1 mile
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download