Swift 使用MKMapView获取方向和路线
我一直在尝试使用MKMapView获取方向和路线,而不使用地图或谷歌地图应用程序。 我想在我的应用程序中使用它并打开其他应用程序。 有人能帮忙吗 这是我所拥有的,但我想更改它,以便在我的应用程序上获得方向和路线。我有Swift 4和Xcode 9Swift 使用MKMapView获取方向和路线,swift,mapkit,Swift,Mapkit,我一直在尝试使用MKMapView获取方向和路线,而不使用地图或谷歌地图应用程序。 我想在我的应用程序中使用它并打开其他应用程序。 有人能帮忙吗 这是我所拥有的,但我想更改它,以便在我的应用程序上获得方向和路线。我有Swift 4和Xcode 9 //Getting direction of location @objc func getDirections(){ if let selectedPin = selectedPin { let map
//Getting direction of location
@objc func getDirections(){
if let selectedPin = selectedPin {
let mapItem = MKMapItem(placemark: selectedPin)
let launchOptions = [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]
mapItem.openInMaps(launchOptions: launchOptions)
}
}
extension ViewController : MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{
if annotation is MKUserLocation {
//return nil so map view draws "blue dot" for standard user location
return nil
}
let reuseId = "pin"
var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView?.pinTintColor = UIColor.red
pinView?.canShowCallout = true
let smallSquare = CGSize(width: 30, height: 30)
let button = UIButton(frame: CGRect(origin: CGPoint.zero, size: smallSquare))
button.setBackgroundImage(UIImage(named: "Car"), for: .normal)
button.addTarget(self, action: #selector(getDirections), for: .touchUpInside)
pinView?.leftCalloutAccessoryView = button
return pinView
}
}
我一直在尝试使用MKMapView获取方向和路线,而不使用地图或谷歌地图应用程序。
我想在我的应用程序中使用它并打开其他应用程序。有人能帮忙吗
您无法在应用程序中启动导航。您可以做的是在两点之间绘制一条路径,并获取距离或预期驾驶/步行时间等信息
搜索两个点之间的距离MapKit,您将找到一组结果。例如看一看,或者