我目前正在IOS项目中,我必须计算两个位置之间的距离。我已经完成了不使用Google的操作,但是我想使用Google API来获取准确的距离,我在这里共享我的代码

    let myLocation = CLLocation(latitude: CLLocationDegrees(latittude[indexPath.row])!, longitude: CLLocationDegrees(longittude[indexPath.row])!)
        let lat = UserDefaults.standard.string(forKey: "lat") ?? ""
        let long = UserDefaults.standard.string(forKey: "long") ?? ""
        let myBuddysLocation = CLLocation(latitude: CLLocationDegrees(lat)!, longitude: CLLocationDegrees(long)!)

最佳答案

使用CoreLocation Framework的 distance 功能,

 var startLocation = CLLocation(latitude: startLatitude, longitude: startLongitude)
 var endLocation = CLLocation(latitude: endLatitude, longitude: endLongitude)
 var distance: CLLocationDistance = startLocation.distance(from: endLocation)

关于ios - 如何在Swift 4中使用Google API计算两个位置之间的距离,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55177604/

10-11 02:39