如何在Swift 4中使用Google API计算两个位置之间的距离

时间:2019-03-15 07:29:44

标签: ios swift google-maps geolocation geocoding

我目前正在从事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)!)

1 个答案:

答案 0 :(得分:1)

使用CoreLocation Framework的距离功能

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