我目前一直在尝试使用 Google map ,并发现了 Apple 提供的一种可用于 CoreLocation 的方法。
方法是:
func distance(from: CLLocation) 返回从接收器位置到指定位置的距离(以米为单位)。
现在最让我感兴趣的是,在将米转换为英里之后,该方法如何得出输出结果。我将结果与谷歌地图计算出的距离进行了比较,发现有很大不同。我的问题是距离函数是否计算出直线距离并忽略转弯(即道路、高速公路等),这本质上会增加计算的距离。
这是我的实现,使用上面提到的方法。
func testDistance(lat0: Double, long0: Double, lat1: Double, long1: Double){
let coordinate₀ = CLLocation(latitude: lat0, longitude: long0)
let coordinate₁ = CLLocation(latitude: lat1, longitude: long1)
let distanceInMeters = coordinate₀.distance(from: coordinate₁)
let distanceInMile = distanceInMeters/1609.344
print("\(distanceInMile) miles")
}
最佳答案
CLLocation 的 distance(from: CLLocation)
函数使用两个位置之间的直线进行计算(考虑到地球的曲率)。
https://developer.apple.com/documentation/corelocation/cllocation/1423689-distance
This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth. The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations.
关于swift - CLLocation 类 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49565684/