如何在用户走过道路时清除折线(折线)

How to clear polyline as the user walks through the road (polyline)

我是地图技术的新手。 我正在使用 google api 到 json 数据,以使用 polyline 获取地图上的路线。 我需要一些关于 polyline 的帮助。我想根据用户驾驶路线清除多段线的路径。我尝试搜索许多解决方案但无法为我工作。 我附上了相同的图像......

绘制折线的函数---

 func drawPath(from polyStr: String){
        print("inside Drawpath")
        path = GMSPath(fromEncodedPath: polyStr)!
        let polyline = GMSPolyline(path: path)

        polyline.strokeWidth = 6
        polyline.map = myMapView // Google MapView
        polyline.strokeColor = UIColor(red: 0, green: 128/255, blue: 1, alpha: 1)

        //   let camera = GMSCameraUpdate.fit(GMSCoordinateBounds(coordinate: CLLocationCoordinate2D(latitude: Double(lat), longitude: Double(long)), coordinate: CLLocationCoordinate2D(latitude: Double(directionlat)!, longitude: Double(directionlat)!)))
        let cameraUpdate = GMSCameraUpdate.fit(GMSCoordinateBounds(coordinate: CLLocationCoordinate2D(latitude: Double(lat), longitude: Double(long)), coordinate: CLLocationCoordinate2D(latitude: Double(directionlat)!, longitude: Double(directionlat)!)))


    //    self.timer = Timer.scheduledTimer(timeInterval: 0.003, target: self, selector: #selector(animatePolylinePath), userInfo: nil, repeats: true)


        //myMapView.moveCamera(cameraUpdate)
        let currentZoom = myMapView.camera.zoom
      //  myMapView.animate(toZoom: currentZoom - 1.4)
    }

感谢任何帮助。 感谢和问候

我遇到了同样的问题,我找到了一个在我当前的 2 个项目中运行良好的解决方案。所以让我在这里分享我的想法。 首先,从方向 API 获取初始的 polypathString。现在您可以通过使用 GMSPath object 并将其与 CLLocation.distance 方法相结合来获取路径中的点数 我试图找出 driver 是否在路径中。

If he is found on the path 
I take the index from array in which the driver is close and start drawing from there 
else 
I request to fetch an another direction API since he is not the path drawing it is not right.

let polyPath = GMSPath.init(fromEncodedPath: pathStr)!


func isPathChanged(byDriver coordinate : CLLocation) -> Bool{
            guard self.path.count() != 0 else{return true}

            for range in 0..<path.count(){
                let point = path.coordinate(at: range).location//CLLocation
                if point.distance(from: coordinate) < 75{
                    self.driversPositiionAtPath = range
                    return false
                }
            }
            self.driversPositiionAtPath = 0
            return true
        }

不只是变魔术 注意: 我正在维护一个变量来存储 driver 在折线 driversPositiionAtPath [=12= 的位置]

if isPathChanged(byDriver : driversLocation){
     self.wsToFetchNewPathFromDirection()
}else{
     self.drawRoute(for : polyPath )
}
func drawRoute(for path : GMSPath){
        let drawingPath = GMSMutablePath()

        for i in self.driversPositiionAtPath..<path.count(){
            drawingPath.add(path.coordinate(at: i))
        }
        self.polyline.path = drawingPath
        self.polyline.strokeColor = UIColor.black
        self.polyline.strokeWidth = 3.0
        self.polyline.map = mapLocation
    }

检查 driver 是否在路径上,一旦他移动或每 10 秒检查一次。 编码愉快!