Select to view content in your preferred language

Route Task solve route is not working

459
1
06-28-2023 06:25 AM
KAMLESHDURGUDE
Emerging Contributor

func displayRoute(){

        let routeTask = AGSRouteTask(url: URL(string: "https://route-api.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World")!)

        let sourceLocation = AGSPoint(x: 73.2368, y: 19.1668, spatialReference: .wgs84())

        let destinationLocation = AGSPoint(x: 74.7480, y: 19.0948, spatialReference: .wgs84())

 

        routeTask.defaultRouteParameters { routeParameters, error in

            if let routeParameters = routeParameters {

                routeParameters.outputSpatialReference = self.mapView.spatialReference

                routeParameters.returnDirections = true

                let start = AGSStop(point: sourceLocation)

                let end = AGSStop(point: destinationLocation)

                routeParameters.setStops([start, end])

 

                routeTask.solveRoute(with: routeParameters) { routeResult, error in

                    if error != nil{

                                    print(error)

                                }else if let routeGeometry = routeResult?.routes.first?.routeGeometry{

                                    let routeSymbol = AGSSimpleLineSymbol(style: .solid, color: .blue, width: 5.0)

                                                    let routeGraphic = AGSGraphic(geometry: routeGeometry, symbol: routeSymbol)

                                                    self.graphicOverlay.graphics.add(routeGraphic)

                                                    let extent = routeGeometry.extent

                                                    self.mapView.setViewpointGeometry(extent)

                                                }

                                            }

                }

            }

        }

Please find above the code to display route but routetask.solveroute is not working. Not even getting any error not in command prompt also. Please help me in this case. How can we show the route in is swift. Please tell me if i am missing anything in the code

0 Kudos
1 Reply
Nicholas-Furness
Esri Regular Contributor

Your routeTask instance is being deallocated before the call can complete. Create an object level variable to hold on to the instance. See this sample code.

Things like this are avoided with Swift Concurrency. You could use this library, which adds Swift Concurrency to most of the ArcGIS Runtime SDK for iOS (here's what solving a route looks like). Alternatively, its successor (ArcGIS Maps SDK for Swift) supports Swift Concurrency out of the box.