You can call a function of the MKMapItem class passing the elements there, it uses only the first and last for the source / destination, respectively, if you want to pass more than two elements.
Swift 5, 4
let source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lng))) source.name = "Source" let destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lng))) destination.name = "Destination" MKMapItem.openMaps(with: [source, destination], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])
or using the extension:
extension MKMapItem { convenience init(coordinate: CLLocationCoordinate2D, name: String) { self.init(placemark: .init(coordinate: coordinate)) self.name = name } } let source = MKMapItem(coordinate: .init(latitude: lat, longitude: lng), name: "Source") let destination = MKMapItem(coordinate: .init(latitude: lat, longitude: lng), name: "Destination") MKMapItem.openMaps( with: [source, destination], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])
dimpiax Apr 14 '18 at 23:34 2018-04-14 23:34
source share