I Draw a poly line on google maps in my app i want to draw a dotted path the gap b/w poly line and my marker

48 Views Asked by At

every one i tried multiple method but there is always gap b/w my poly line and drawn marker where my route end. i want to draw dotted path the gap b/w my polyline and marker i also tried walking url but still it doesnot work please help how can i fill this gap that always here what ever. lat and long. i use for. both polyy liine and marker settup black one for driving and green one for walking.

enter image description here

extension MapViewController: SetUpLocationDisplayLogic {
    func mapRouteSetup(startlatitude: Double, startlongitude: Double, endlatitude: Double, endlongitude: Double) {
        let apiKey = "API_KEY"
        
        // Define the URLs for walking and driving routes
        let walkingRequestURL = "https://maps.googleapis.com/maps/api/directions/json?origin=\(startlatitude),\(startlongitude)&destination=\(endlatitude),\(endlongitude)&mode=walking&key=\(apiKey)"
        
        let drivingRequestURL = "https://maps.googleapis.com/maps/api/directions/json?origin=\(startlatitude),\(startlongitude)&destination=\(endlatitude),\(endlongitude)&key=\(apiKey)"
        
        // Make API requests for both walking and driving routes
        URLSession.shared.dataTask(with: URL(string: walkingRequestURL)!) { (data, response, error) in
            if let data = data {
                // Parse and draw walking route
                self.drawWalkingRoute(data: data)
            } else if let error = error {
                print("Error making the walking route API request: \(error)")
            }
        }.resume()
        
        URLSession.shared.dataTask(with: URL(string: drivingRequestURL)!) { (data, response, error) in
            if let data = data {
                // Parse and draw driving route
                self.drawDrivingRoute(data: data)
            } else if let error = error {
                print("Error making the driving route API request: \(error)")
            }
        }.resume()
    }

    func drawWalkingRoute(data: Data) {
        if let path = createPathFromRouteData(data) {
            DispatchQueue.main.async {
                // Create a GMSPolyline for the walking route
                let polyline = GMSPolyline(path: path)
                polyline.strokeColor = .appColor(.x8DC63F) // Customize the color as needed
                polyline.strokeWidth = 10.0 // Customize the width as needed
                polyline.map = self.mapView
            }
        }
    }
    
    func drawDrivingRoute(data: Data) {
        if let path = createPathFromRouteData(data) {
            DispatchQueue.main.async {
                // Create a GMSPolyline for the driving route
                let polyline = GMSPolyline(path: path)
                polyline.strokeColor = .appColor(.x434343) // Customize the color as needed
                polyline.strokeWidth = 10.0 // Customize the width as needed
                polyline.map = self.mapView
            }
        }
    }
    
    func createPathFromRouteData(_ routeData: Data) -> GMSPath? {
        do {
            let jsonObject = try JSONSerialization.jsonObject(with: routeData, options: [])
            
            if let jsonDict = jsonObject as? [String: Any],
               let routes = jsonDict["routes"] as? [[String: Any]],
               let firstRoute = routes.first,
               let overviewPolyline = firstRoute["overview_polyline"] as? [String: Any],
               let points = overviewPolyline["points"] as? String {
                
                let path = GMSPath(fromEncodedPath: points)
                return path
            }
        } catch {
            print("Error parsing route data: \(error)")
        }
        
        return nil
    }
    
    
    func didFindLocation(startaddress: String, endaddress: String, startlatitude: Double, startlongitude: Double, endlatitude: Double, endlongitude: Double) {
        DispatchQueue.main.async { [weak self] in
            self?.startingTextField.text = startaddress
            self?.endingTextField.text = endaddress
            
            let camera = GMSCameraPosition.camera(
                withLatitude: startlatitude,
                longitude: startlongitude,
                zoom: 12.0
            )
            self?.mapView.camera = camera
            
            let firstMarkerPosition = CLLocationCoordinate2D(
                latitude: startlatitude,
                longitude: startlongitude
            )
            self?.showMarker(
                position: firstMarkerPosition,
                title: "First Location",
                snippet: "Another Location",
                icon: (self?.customMarkerImage ?? self?.placeHolder)!
            )
            
            let secondMarkerPosition = CLLocationCoordinate2D(
                latitude: endlatitude,
                longitude: endlongitude
            )
            self?.showMarker(
                position: secondMarkerPosition,
                title: "Second Location",
                snippet: "Another Location",
                icon: (self?.customMarkerImage ?? self?.placeHolder)!
            )
            
            // Call the mapRouteSetup function with the provided latitude and longitude
            self?.mapRouteSetup(startlatitude: startlatitude, startlongitude: startlongitude, endlatitude: endlatitude, endlongitude: endlongitude)   
        }   
    }
}
0

There are 0 best solutions below