Search code examples
swiftmapsdirections

Function to open apple maps for driving directions?


I am constructing an app that features a list of businesses and their locations (locations in coordinate form), and I'd like to provide a button that, when pressed, pushes the user over to Apple Maps and brings up driving directions.

How could I go about doing this? I've seen similar questions asked on StackOverflow, but they've been referring to mobile web URLS, mine will be native to the application.

Thanks!


Solution

  • Yes you can do this. Check out this code.

        let placemark : MKPlacemark = MKPlacemark(coordinate: <your coordinate>, addressDictionary:nil)
        //your placemark... Put in the coordinate here
        let mapItem:MKMapItem = MKMapItem(placemark: placemark)
        let launchOptions: NSDictionary = NSDictionary(object: MKLaunchOptionsDirectionsModeWalking, forKey: MKLaunchOptionsDirectionsModeKey)
        //in the lauch options, you can make it driving, walking, or public transportation
        MKMapItem.openMapsWithItems([currentLocationMapItem, mapItem], launchOptions: launchOptions as? [String : AnyObject])