【问题标题】:How to calculate distance between two locations using google api in swift 4如何在swift 4中使用google api计算两个位置之间的距离
【发布时间】:2019-08-06 05:43:08
【问题描述】:

我目前正在做一个 IOS 项目,我必须计算两个位置之间的距离。我没有使用谷歌,但我想使用谷歌 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)!)

【问题讨论】:

标签: ios swift google-maps geolocation geocoding


【解决方案1】:

使用CoreLocation框架的距离功能,

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

【讨论】:

  • 它计算出准确的距离吗?
  • 距离的计算方法是在两点之间沿着地球曲率绘制一条线,并测量所得弧的长度。developer.apple.com/documentation/corelocation/cllocation/…
  • @Yatendra 是否意味着它给出了准确的距离?
  • 它给出了准确的直线距离,但以驾驶为例会不准确。所以这取决于你的需求。
猜你喜欢
  • 1970-01-01
  • 2016-01-01
  • 2018-09-21
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 2012-12-12
  • 1970-01-01
  • 2014-05-15
相关资源
最近更新 更多