I would like to display the route from the one location to a second location. I have created some methods. The first one is to getDirection(), second is to createDirectionRequest and the rendererFor method. I am calling the getDirections method in my viewDidLoad. However when I run it, it seems that nothing is getting call. I added a print statement in both methods but they are not getting display on the console. Is it related to the thread.
Thanks for all replies and explanations
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
locationManager.startUpdatingLocation()
mapViewOutlet.delegate = self
getDirections()
settingImageRating()
func getDirections (){
guard let location = locationManager.location?.coordinate else {return}
let request = createDirectionRequest(from: location)
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
for route in unwrappedResponse.routes {
self.mapViewOutlet.addOverlay(route.polyline)
self.mapViewOutlet.setVisibleMapRect(route.polyline.boundingMapRect, animated: true)
}
print("get direction")
}
}
func createDirectionRequest (from coordinate: CLLocationCoordinate2D) -> MKDirections.Request{
let doubleLatitude = NSDecimalNumber(decimal: (business?.coordinates.latitude)!).doubleValue
let doubleLongitude = NSDecimalNumber(decimal: (business?.coordinates.longitude)!).doubleValue
let request = MKDirections.Request()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: locationManager.location!.coordinate))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: doubleLatitude, longitude: doubleLongitude), addressDictionary: nil))
print(request.source!)
print(request.destination!)
request.requestsAlternateRoutes = true
request.transportType = .walking
print("create direction")
return request
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolylineRenderer(polyline: overlay as! MKPolyline)
renderer.strokeColor = UIColor.blue
return renderer
}