Search code examples
swiftxcodepolylinegmsmapview

How to clear polyline as the user walks through the road


I am new to maps technology. I am using Google apis to the the json data to fetch the routes on the map using the polyline.

I need some help regarding the polyline. I want to clear the path of polyline as per users drives through the route. I tried searching many solutions but they didn't work for me. I have attached an image for the same:

Screenshot of the path

Function to draw 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)
    }
    

What can I try next?


Solution

  • I have faced the same issue and I found a solution which works fine in my current 2 projects. So let me share my idea here. first, get the initial polypathString from the direction API. Now you can get the number of points in the path by using GMSPath object and by combining that with CLLocation.distance method I have tried to find whether the driver is in the path.

    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
            }
    

    not just do the magic trick Note: I am maintaining a variable to store the driver's position at polyline driversPositiionAtPath

    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
        }
    

    Check if driver is on the path or not, once he moves or every 10 seconds. happy codding !