19

I am developing an application needs to open an Apple Maps session, and pass in latitude and longitude coordinates to get directions to that location from a users current location.

I know this can be done in google maps which I am already doing, but when attempting to open the URL in Apple Maps it just opens the place not the directions from a users current location to their destination.

Here is the URL scheme I have been using:

http://maps.apple.com/?ll=(someLatitude),(someLongitute)

Code:

UIApplication.sharedApplication().openURL(NSURL(string:"http://maps.apple.com/?ll=\(locationLat),\(locationlong)")!)

Any help would be greatly appreciated. Thanks!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
randomorb2110
  • 259
  • 1
  • 4
  • 9

3 Answers3

27

Try this code, AppleMap will open up with the directions marked from device's current location to the location specified the coordinates.

        let coordinate = CLLocationCoordinate2DMake(currentLat, currentLong)
        let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil))
        mapItem.name = “Destination/Target Address or Name”
        mapItem.openInMapsWithLaunchOptions([MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving])
Prabhu.Somasundaram
  • 1,380
  • 10
  • 13
6

try use

NSURL(string:"http://maps.apple.com/?saddr=\(currentLat),\(currentLong)&daddr=\(destinationLat),\(destinationLong)")!

with currentLat currentLong is users current location, and destinationLat destinationLong is destination location.

more parameters (ex: the transport type) look at here

larva
  • 4,687
  • 1
  • 24
  • 44
1

Based on the larva's answer but I recommend you to use "maps://" URL scheme instead of "http://maps.apple.com/" to bypass opening Safari.

Like here:

URL(string: "maps://?saddr=\(currentLat),\(currentLong)&daddr=\(destinationLat),\(destinationLong)")
Nikaaner
  • 1,022
  • 16
  • 19