import UIKit
import MapKit
class ViewController: UIViewController {
var geoCoder = CLGeocoder()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
geoCoder.geocodeAddressString("北京") { (startCLPLS : [CLPlacemark]?,error : Error?) in
self.geoCoder.geocodeAddressString("珠海") { (endCLPLS : [CLPlacemark]?,error : Error?) in
self.beginNav(startCLPL: (startCLPLS?.first)!, endCLPL: (endCLPLS?.first)!)
}
}
}
/// 开始导航
///
/// - Parameters:
/// - startCLPL: 开始地址
/// - endCLPL: 结束地址
func beginNav(startCLPL : CLPlacemark,endCLPL : CLPlacemark) {
let startItem = MKMapItem(placemark: MKPlacemark(placemark: startCLPL))
let endItem = MKMapItem(placemark: MKPlacemark(placemark: endCLPL))
let mapItems : [MKMapItem] = [startItem,endItem]
let dict : [String : AnyObject] = [
MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving as AnyObject,
MKLaunchOptionsMapTypeKey:MKMapType.standard.rawValue as AnyObject,MKLaunchOptionsShowsTrafficKey:true as AnyObject]
MKMapItem.openMaps(with: mapItems, launchOptions: dict)
}
}
网友评论