Reputation: 128
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
}
Upvotes: -2
Views: 165
Reputation: 542
Can you please check that you set delegate inStroryboard
for MKMapView and in UIViewController
file set delegate like class ViewController: UIViewController, MKMapViewDelegate
.
I tried this code and it's working fine for me.
import UIKit
import MapKit
class ViewController: UIViewController, MKMapViewDelegate{
@IBOutlet weak var MKMapVW: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.showRouteOnMap()
}
func showRouteOnMap() {
let coordinateOne = CLLocationCoordinate2D(latitude: CLLocationDegrees(exactly: 40.586746)!, longitude: CLLocationDegrees(exactly: -108.610891)!)
let coordinateTwo = CLLocationCoordinate2D(latitude: CLLocationDegrees(exactly: 42.564874)!, longitude: CLLocationDegrees(exactly: -102.125547)!)
let request = MKDirections.Request()
request.source = MKMapItem(placemark: MKPlacemark(coordinate: coordinateOne, addressDictionary: nil))
request.destination = MKMapItem(placemark: MKPlacemark(coordinate: coordinateTwo, addressDictionary: nil))
request.requestsAlternateRoutes = true
request.transportType = .automobile
let directions = MKDirections(request: request)
directions.calculate { [unowned self] response, error in
guard let unwrappedResponse = response else { return }
if (unwrappedResponse.routes.count > 0) {
self.MKMapVW.addOverlay(unwrappedResponse.routes[0].polyline)
self.MKMapVW.setVisibleMapRect(unwrappedResponse.routes[0].polyline.boundingMapRect, animated: true)
}
}
}
func mapView(_ mapView: MKMapView!, rendererFor overlay: MKOverlay!) -> MKOverlayRenderer! {
if overlay is MKPolyline {
let polylineRenderer = MKPolylineRenderer(overlay: overlay)
polylineRenderer.strokeColor = UIColor.blue
polylineRenderer.lineWidth = 5
return polylineRenderer
}
return nil
}
}
Upvotes: 0