使用我的 ios 应用 iOS 9 Swift 2.2 中的指示打开 Apple 地图应用

Open Apple Maps App With Directions from my ios App iOS 9 Swift 2.2

我一直在寻找很多教程\帮助但似乎没有找到任何,所以我再次来到这里.... 我想打开 Apple 地图以获取路线并从用户当前位置导航到选定的 Pin 当我在我的应用程序中按下自定义 leftCalloutAccessory

我已经设置了按钮,但无法使该功能正常工作,所以请'如果有人可以指导我完成或帮助我使用代码,那将是救命稻草!谢谢

这是我的代码:

    import UIKit
    import MapKit
    import CoreLocation

    class MapViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate,UISearchBarDelegate{

    @IBOutlet weak var mapView: MKMapView!

    let locationManager = CLLocationManager()

        override func viewDidLoad() {
            super.viewDidLoad()


            //==========RegionLocation : =========

            // Init the zoom level
            let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 31.30, longitude: 34.45)
            let span = MKCoordinateSpanMake(125, 125)
            let region = MKCoordinateRegionMake(coordinate, span)
            self.mapView.setRegion(region, animated: true)



            //====================================\
    }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        //Dispose of resources that can be re created.

            }

        //Mark : Location

        func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])



        {
            let location = locations.last

            let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.06, longitudeDelta: 0.06))

            self.mapView.setRegion(region, animated: true)

            self.locationManager.stopUpdatingLocation()
        }

        func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
        {
            print("Errors: " + error.localizedDescription)
        }

        func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
            if annotation is MKUserLocation {
                return nil
            }
            let reuseID = "pin"
            var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
            if(pinView == nil) {
                pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
                pinView!.canShowCallout = true
                pinView!.animatesDrop = true
                pinView!.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIButton
                let smallSquare = CGSize(width: 30, height: 30)
                let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
                button.setBackgroundImage(UIImage(named: "Car"), forState: .Normal)
                pinView?.leftCalloutAccessoryView = button
            }
            else
            {
                pinView!.annotation = annotation
            }


            return pinView

        }

        func mapView(MapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped Control: UIControl) {

            if Control == annotationView.leftCalloutAccessoryView {

            }

}

以下函数将打开 Apple 地图应用程序并显示从用户当前位置到坐标指定目的地的行车路线。目的地名称显示在地图应用的 To: 字段中。

func openMapsAppWithDirections(to coordinate: CLLocationCoordinate2D, destinationName name: String) {
  let options = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
  let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
  let mapItem = MKMapItem(placemark: placemark)
  mapItem.name = name // Provide the name of the destination in the To: field
  mapItem.openInMapsWithLaunchOptions(options)
}

您可以从您的代码中调用此函数,如下所示:

func mapView(MapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped Control: UIControl) {

  if Control == annotationView.leftCalloutAccessoryView {
    if let annotation = annotationView.annotation {
      // Unwrap the double-optional annotation.title property or
      // name the destination "Unknown" if the annotation has no title
      let destinationName = (annotation.title ?? nil) ?? "Unknown"
      openMapsAppWithDirections(to: annotation.coordinate, destinationName: destinationName)
    }
  }

}

像这样创建扩展:

extension CLLocation {

    func drive() {

        let placemark = MKPlacemark(coordinate: self.coordinate, addressDictionary: addressDictionary)

        let launchOptions = [
            MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving
        ]

        MKMapItem.openMaps(with: [MKMapItem.forCurrentLocation(), MKMapItem(placemark: placemark)], launchOptions: launchOptions)

    }
}