import UIKit//1.导入框架
import MapKitclass ViewController: UIViewController {lazy var geoCoder : CLGeocoder = {return CLGeocoder()}()override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {geoCoder.geocodeAddressString("广州") { (pls, err) in//广州坐标guard let gzPL = pls?.first else{return}self.geoCoder.geocodeAddressString("上海", completionHandler: { (pls, err) in//上海坐标if let shPL = pls?.first{self.beginNav(startPLCL: gzPL, endPLCL: shPL)}})}}
}// MARK: - 导航起点和终点
extension ViewController{func beginNav(startPLCL: CLPlacemark, endPLCL: CLPlacemark) {// 起点let plMK: MKPlacemark = MKPlacemark(placemark: startPLCL)let startItem: MKMapItem = MKMapItem(placemark: plMK)// 终点let endplMK: MKPlacemark = MKPlacemark(placemark: endPLCL)let endItem: MKMapItem = MKMapItem(placemark: endplMK)// 起点和终点let mapItems: [MKMapItem] = [startItem, endItem]// 导航设置字典let dic: [String : Any] = [// 导航模式MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving,// 地图样式MKLaunchOptionsMapTypeKey: MKMapType.standard.rawValue,// 显示交通MKLaunchOptionsShowsTrafficKey: true]MKMapItem.openMaps(with: mapItems, launchOptions: dic)}
}