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?