Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Open Apple Maps with latitude and longitude directions

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!

like image 542
randomorb2110 Avatar asked Jul 13 '16 17:07

randomorb2110


People also ask

Can you enter latitude and longitude into Apple Maps?

Enter coordinates to find a placeIn the search box, enter your coordinates. Here are examples of formats that work: Decimal degrees (DD): 41.40338, 2.17403. Degrees, minutes, and seconds (DMS): 41°24'12.2"N 2°10'26.5"E.

Can you make custom directions on Apple Maps?

In the Maps app on your Mac, click a location on the map, such as an intersection, landmark, or business. In the place card, do one of the following: Click Create Route, then enter the destination in the To field (or click the Swap Directions button , then enter the starting point in the From field).

How do I open directions in Apple Maps?

Tap your destination (such as a search result in Maps or a landmark on a map), or touch and hold anywhere on the map, then tap the directions button.


2 Answers

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])
like image 58
Prabhu.Somasundaram Avatar answered Oct 25 '22 00:10

Prabhu.Somasundaram


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

like image 37
larva Avatar answered Oct 25 '22 00:10

larva