MKPolyline not following the street?

Hi Developers, i have this problem i set my polyline as(i'm learning, don't really know if this draw the line thru roads)


self.map.addOverlay(testline, level: .aboveRoads)


.adoveRoads to follow the 'road' but it is not following, just drawing a line to the annotation.


so here's my code


self.enviarCotizacion.isHidden = false
                        self.cotizacionUitext.isHidden = false
                        self.accionEnviar.isHidden = false
                        self.Destino.isHidden = false
                        self.destinoContainer.isHidden = false
                      
                        var latDelta:CLLocationDegrees = 0.01
                      
                        var longDelta:CLLocationDegrees = 0.01
                      
                        let theSpan:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)
                        let pointLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitud2blecontuctor, longitud2bleconductor)
                            let region:MKCoordinateRegion = MKCoordinateRegion(center: pointLocation, span: theSpan)
                        self.map.setRegion(region, animated: true)
                      
                          
                      
                        let pasajero : CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitud2blepasajero, longitud2blepasajero)
                      
                        let objectAnnotation = MKPointAnnotation()
                        objectAnnotation.coordinate = pasajero
                        objectAnnotation.title = "Pasajero"
                        self.map.addAnnotation(objectAnnotation)
                          
                          
                          
                          // this is where i set my CLLOCATIONS to my Polyline 
                            let destino : CLLocationCoordinate2D = CLLocationCoordinate2DMake(article.Latitud_D, article.Longitud_D)
                          
                            let objectAnnotationn = MKPointAnnotation()
                            objectAnnotationn.coordinate = destino
                            objectAnnotationn.title = "Destino"
                            self.map.addAnnotation(objectAnnotationn)
                          
                            // diferrent cords to on my polyline to follow 
                            let coords1 = CLLocationCoordinate2D(latitude: latitud2blecontuctor, longitude: longitud2bleconductor)
                            let coords2 = CLLocationCoordinate2D(latitude: latitud2blepasajero, longitude: longitud2blepasajero)
                            let coords3 = CLLocationCoordinate2D(latitude: article.Latitud_D, longitude: article.Longitud_D)
                            let testcoords:[CLLocationCoordinate2D] = [coords1,coords2,coords3]
                          // my polyline 
                            let testline = MKPolyline(coordinates: testcoords, count: testcoords.count)
                           

                            self.map.addOverlay(testline, level: .aboveRoads)
                          
                            self.map.delegate = self
                            self.map.centerCoordinate = pasajero
                            self.map.region = MKCoordinateRegion(center: coords2, span: MKCoordinateSpan(latitudeDelta: 0.02, longitudeDelta: 0.02))
                      

                             break
                        }
                    }
                      
                    else if  (blog.Response == "False") {
                   
                    }
                  
                }
              
            }
            catch {
                print("Error: Couldn't decode data into Blog:", error)
                print(error)
                return
            }
        }
        task.resume()
      
      
    }
  
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
      
        if let polyline = overlay as? MKPolyline {
            let testlineRenderer = MKPolylineRenderer(polyline: polyline)
            testlineRenderer.strokeColor = .blue
            testlineRenderer.lineWidth = 2.0
            return testlineRenderer
        }
        fatalError("Something wrong...")
      
    }

Accepted Reply

found the solution


self.map.delegate = self
                            self.map.centerCoordinate = pasajero
                            self.map.region = MKCoordinateRegion(center: coords2, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005))
                            
                            
                            let directionsRequest = MKDirections.Request()
                            directionsRequest.source = MKMapItem(placemark: MKPlacemark(coordinate: self.map.userLocation.coordinate))
                            directionsRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate: destino))
                            directionsRequest.requestsAlternateRoutes = true
                        
                            directionsRequest.transportType = .any
                            let directions = MKDirections(request: directionsRequest)
                            
                            directions.calculate { response, error in
                                if let res = response {
                                    if let route = res.routes.first {
                                        self.map.addOverlay(route.polyline)
                                        self.map.region.center = destino
                                    }
                                } else {
                                    print("error")
                                }
                            }
                             break

Replies

found the solution


self.map.delegate = self
                            self.map.centerCoordinate = pasajero
                            self.map.region = MKCoordinateRegion(center: coords2, span: MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005))
                            
                            
                            let directionsRequest = MKDirections.Request()
                            directionsRequest.source = MKMapItem(placemark: MKPlacemark(coordinate: self.map.userLocation.coordinate))
                            directionsRequest.destination = MKMapItem(placemark: MKPlacemark(coordinate: destino))
                            directionsRequest.requestsAlternateRoutes = true
                        
                            directionsRequest.transportType = .any
                            let directions = MKDirections(request: directionsRequest)
                            
                            directions.calculate { response, error in
                                if let res = response {
                                    if let route = res.routes.first {
                                        self.map.addOverlay(route.polyline)
                                        self.map.region.center = destino
                                    }
                                } else {
                                    print("error")
                                }
                            }
                             break