我要使其显示两个CLLocation坐标之间的距离量。没有复杂的数学公式,有办法做到这一点吗?如果没有,您将如何使用公式?
最佳答案
CLLocation具有distanceFromLocation方法,因此给定了两个CLLocations:
CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];
或在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
您会在此处到达距离(以为单位),因此 1英里 = 1609米
if(distanceInMeters <= 1609)
{
// under 1 mile
}
else
{
// out of 1 mile
}