How to open Apple Maps driving direction by passing address

3.8k views Asked by At

I'm trying to implement a function that open the Apple Map app's driving direction screen by passing actual address.

The address is not just random addresses but business addresses which are mostly registered and searchable on Apple maps. So by passing the address, it should be matched and correctly show direction to that business instead of pointing to an unknown annotation on the map. Such case happens when I pass longitude and latitude directly so I don't want to use geocodeAddressString() to convert the address to geocoordination.

How can I achieve it?

2

There are 2 answers

5
Nicola Giancecchi On BEST ANSWER

Simply use the Apple Maps API. If you can find a business by tiping its name in Apple Maps, you can find it through the APIs. In your case, the correct parameter is daddr, like this:

http://maps.apple.com/?daddr=1+Infinite+Loop,+Cupertino,+CA

You can combine multiple parameters, such as your starting location:

http://maps.apple.com/?saddr=1024+Market+St,+San+Francisco,+CA&daddr=1+Infinite+Loop,+Cupertino,+CA

You can find the list of supported parameters here.

Remember to open the URL via UIApplication.shared().open(url: URL, options: [String: AnyObject], completionHandler: ((Bool) -> Void)?) - in iOS 10 - or UIApplication.shared.open(url: URL)

5
Daij-Djan On

You could call maps with a url (as detailed by Nicola) but there is also a proper api for this : MKMapItem

  1. basically you create map items for POIs / addresses [1 or more]
  2. open Maps with them [use the launchOptions for starting with directions]

    func showMap() {
        //---
        //create item 1
    
        //address
        let coords = CLLocationCoordinate(coordinateField.doubleValue!,coordinateField.doubleValue!)
        let addressDict =
               [CNPostalAddressStreetKey: address.text!,
                CNPostalAddressCityKey: city.text!,
                CNPostalAddressStateKey: state.text!,
                CNPostalAddressPostalCodeKey: zip.text!]        
        //item
        let place = MKPlacemark(coordinate: coords!,
                                 addressDictionary: addressDict)
        let mapItem = MKMapItem(placemark: place)
    
        //---
        //create item 2
    
        //address
        let coords2 = CLLocationCoordinate(coordinateField2.doubleValue!,coordinateField2.doubleValue!)
        let addressDict2 =
               [CNPostalAddressStreetKey: address2.text!,
                CNPostalAddressCityKey: city2.text!,
                CNPostalAddressStateKey: state2.text!,
                CNPostalAddressPostalCodeKey: zip2.text!]
        //item2
        let place2 = MKPlacemark(coordinate: coords2!,
                                 addressDictionary: addressDict2)
        let mapItem2 = MKMapItem(placemark: place2)
    
        //-----
        //launch it     
        let options = [MKLaunchOptionsDirectionsModeKey:
                            MKLaunchOptionsDirectionsModeDriving]
    
        //for 1 only.       
        mapItem.openInMaps(launchOptions: options)
        //for 1 or N items 
        var mapItems = [mapItem, mapItem2] //src to destination
        MKMapItem.openMaps(withItems:mapItems, launchOptions: options)
    }