How to find out distance between coordinates?

John Doe picture John Doe · Oct 23, 2015 · Viewed 76k times · Source

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

Glenn Howes picture Glenn Howes · Oct 23, 2015

CLLocation has a distanceFromLocation method so given two CLLocations:

CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];

or in Swift 4:

//: 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
 }
 else
{
 // out of 1 mile
 }