swift - CLLocation 类 Swift

标签 swift maps

我目前一直在尝试使用 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/

相关文章:

ios - 无法在使用 Alamofire 下载的 PDFView IOS 中查看 PDF - SWIFT

android - 谷歌地图不适用于 Android

objective-c - Swift 函数未包含在自动生成的 -Swift.h 伞文件中

ios - 将视频从相机转换为字节数组 Swift

ios - 我试图在退出应用程序时暂停 Sprite Kit 游戏

swift - 获取内部 URLSession 错误

c++ - 如何使用迭代器初始化映射的关键字段?

c++ - 在哪里可以找到 Qt 5 beta 2 的 Qt Location 插件?

java - Android:为谷歌地图设置 API key

r - 如何在R中使用OSRM绘制多个等时线多边形?