【发布时间】:2019-08-02 20:10:12
【问题描述】:
我创建了一张地图,您可以在其中按下开始按钮。然后应用程序将放大到您的当前位置,并每 10 秒更新一次坐标并插入到坐标数组中。一旦我按下停止按钮,我就有一条折线在所有坐标之间画线。 (如下图)
所以我现在的问题是: 如何计算折线的绘制距离?
//Draw polyline on the map
let aPolyLine = MKPolyline(coordinates: self.locations, count: self.locations.count)
//Adding polyline to mapview
self.mapView.addOverlay(aPolyLine)
let startResult = self.locations.startIndex
let stopResult = self.locations.endIndex
//Retrieve distance and convert into kilometers
let distance = startResult.distance(to: stopResult)
let result = Double(distance) / 1000
let y = Double(round(10 * result)) / 10
self.KiloMeters.text = String(y) + " km"
我的猜测是我不能使用 startResult.distnace(to: stopResult) 因为,如果我绕一圈,公里会显示 0?正确的?我不确定,但它仍然有效。像我一样使用代码时没有显示任何内容。
【问题讨论】:
-
添加每个点到下一个点的距离
-
你有一个位置数组。遍历数组,找到每个点与下一个点之间的距离。总这些距离
-
CLLocation 类中有一个
distance(from:)方法 -
不确定 distance(to:) 是什么,但如果它适合你,那么就使用它,否则就像我说的另一个函数在 CLLocation 类中。
-
@Putte,你使用的这个
distance(to:)实际上是Array.Index类型的方法,现在是Int。 And this function just returns difference between two indexes。对于Array中的.startIndex和.endIndex,它只是数组的长度。
标签: ios swift polyline mkpolyline