问题描述
我写了下面的代码来删除用户的行进路径,但它不会按预期工作,有时会正确删除,有时不会。在调试了很多之后,我在这种情况下观察到一件事,那就是当我们调用方向 API 并获取路线点以创建路径时,2 个连续点之间的距离总是不同的,例如我们在 2 个点之间有 10 个路线坐标,但所有坐标都有不同的距离来自上一个。
func updateTravelledpath(currentLoc: CLLocationCoordinate2D){
var index = 0
if let counter = self.route?.path?.count() {
for i in 0..<counter {
let pathLat = Double(self.route?.path?.coordinate(at: i).latitude ?? 0.0).rounded(toPlaces: 5)
let pathLong = Double(self.route?.path?.coordinate(at: i).longitude ?? 0.0).rounded(toPlaces: 5)
let currentLat = Double(currentLoc.latitude).rounded(toPlaces: 5)
let currentLong = Double(currentLoc.longitude).rounded(toPlaces: 5)
if currentLat <= pathLat || currentLong <= pathLong{
index = Int(i)
break //Breaking the loop when the index found
}
}
//Creating new path from the current location to the destination
let newPath = GMSMutablePath()
for i in index..<Int(counter){
if let cordd = self.route?.path?.coordinate(at: UInt(i)) {
newPath.add(cordd)
}
}
self.route?.path = newPath
dispatchQueue.main.async {
for item in self.route?.polyLine ?? [GMSpolyline]() {
item.map = nil
}
self.route?.polyLinee = nil
self.route?.polyLinee?.map = nil
self.route?.polyLinee = GMSpolyline(path: self.route?.path)
self.route?.polyLinee?.strokeWidth = 5.0
self.route?.polyLinee?.strokeColor = #colorLiteral(red: 0.02352941176,green: 0.5725490196,blue: 0.4666666667,alpha: 1)
self.route?.polyLine = [GMSpolyline(path: self.route?.path)]
self.route?.polyLinee?.map = self.mapView
self.mapView.reloadInputViews()
}
}
}
extension Double {
// Rounds the double to decimal places value
func rounded(toPlaces places:Int) -> Double {
let divisor = pow(10.0,Double(places))
return (self * divisor).rounded() / divisor
}
}
**Thanks in advance**
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)