1

All the answers on SO for this question are using Objective-c, or using Swift 2. Any way I have two points, the coordinates for the first one is (lat, long), and for the second point (coordinates.latitude, coordinates.longitude). This is what I was able to get so far, but still I have two problems:

Value of type 'MKDirectionsRequest' has no member 'setSource'
Value of type 'MKDirectionsRequest' has no member 'setDestination'

Any way I got this code from this page:http://studyswift.blogspot.com/2014/10/mkdirections-draw-route-from-location.html

This is my code so far:

@IBOutlet weak var myMap: MKMapView!
var myRoute : MKRoute?

Inside viewDidLoad I have the following:

let directionsRequest = MKDirectionsRequest()
        let markFirstPoint = MKPlacemark(coordinate: CLLocationCoordinate2DMake(lat, long), addressDictionary: nil)
        let markSecondPoint = MKPlacemark(coordinate: CLLocationCoordinate2DMake(coordinates.latitude, coordinates.longitude), addressDictionary: nil)
        directionsRequest.setSource(MKMapItem(placemark: markSecondPoint))
        directionsRequest.setDestination(MKMapItem(placemark: markFirstPoint))
        directionsRequest.transportType = MKDirectionsTransportType.automobile
        let directions = MKDirections(request: directionsRequest)
        directions.calculate { (response:MKDirectionsResponse!, error: Error!) -> Void in
            if error == nil {
                self.myRoute = response.routes[0] as? MKRoute
                self.myMap.add((self.myRoute?.polyline)!)
            }
        }

after viewDidLoad I have this:

 func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        var myLineRenderer = MKPolylineRenderer(polyline: (myRoute?.polyline)!)
        myLineRenderer.strokeColor = UIColor.red
        myLineRenderer.lineWidth = 3
        return myLineRenderer
    }

Is there any way I can get this code to work?

codeDojo
  • 359
  • 3
  • 16

1 Answers1

3

Simply look at the documentation for MKDirectionsRequest. There is a source property and a destination property.

The lines:

directionsRequest.setSource(MKMapItem(placemark: markSecondPoint))
directionsRequest.setDestination(MKMapItem(placemark: markFirstPoint))

become:

directionsRequest.source = MKMapItem(placemark: markSecondPoint)
directionsRequest.destination = MKMapItem(placemark: markFirstPoint)
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks man, it worked. but during the run time it gave me this error: `Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional ` At this line: `self.myMap.add((self.myRoute?.polyline)!)` – codeDojo Nov 12 '17 at 16:12
  • That's an entirely separate issue that you should post a separate question for. But before you do, thoroughly review https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu – rmaddy Nov 12 '17 at 16:14