1

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Deven Nazare
  • 538
  • 5
  • 24

1 Answers1

4

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 !

AlbinMrngStar
  • 289
  • 2
  • 13
  • but how to get location or the distance if the driver moves ?? @albinmrngstar – Deven Nazare Mar 04 '20 at 04:34
  • you can get location from device CLLocation Delegates, And the CLLocation object has a function called distance(from : CLLocation) which returnes distance two cllocation point in meters. check this link https://developer.apple.com/documentation/corelocation/cllocation – AlbinMrngStar Mar 04 '20 at 04:40
  • ``` if isPathChanged(byDriver : driversLocation){ self.wsToFetchNewPathFromDirection() }else{ self.drawRoute(for : polyPath ) } ``` where did you called this ?? and what is this method ? wsToFetchNewPathFromDirection() – Deven Nazare Mar 04 '20 at 04:57
  • from wsToFetchNewPathFromDirection is the method for web service to fetch new path from google Direction API. Din't you get the poly string from google direction API. and about isPathChanged use it in the when you receive new location in location manager delegate (func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])) or once every 10 seconds. – AlbinMrngStar Mar 04 '20 at 05:03
  • If you don't want to face all the above troubles. just hit direction API every 10 seconds with start and end point -> clear your existing path from the map -> draw the new path [NOTE: for every 1000hit google charges 5$] – AlbinMrngStar Mar 04 '20 at 10:48