Anonymous Dev Anonymous Dev - 1 year ago 60
Swift Question

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

i have been looking for a lot of tutorials \ Help but doesn't seem to find any , So i once again Come here ....
I would like to Open Apple Maps to get directions and navigate from the user current location to a selected Pin When i press the custom leftCalloutAccessory in my app

I have set up the Button but Cant get the function to work so please'If anyone can guide me through or help me with Code that will be a life saver ! thx

Here is my Code :

import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate,UISearchBarDelegate{

@IBOutlet weak var mapView: MKMapView!

let locationManager = CLLocationManager()

override func 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() {
//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)


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
pinView!.annotation = annotation

return pinView


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

if Control == annotationView.leftCalloutAccessoryView {



Answer Source

The following function will open the Apple Maps app and present driving directions from the user's current location to a named destination at a coordinate. The destination name is displayed in the To: field of the Maps app.

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

You can call this function from your code as follows:

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)