How to find out distance between coordinates?
Swift 4.1
import CoreLocation
//My location
let myLocation = CLLocation(latitude: 59.244696, longitude: 17.813868)
//My buddy's location
let myBuddysLocation = CLLocation(latitude: 59.326354, longitude: 18.072310)
//Measuring my distance to my buddy's (in km)
let distance = myLocation.distance(from: myBuddysLocation) / 1000
//Display the result in km
print(String(format: "The distance to my buddy is %.01fkm", distance))
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
}