Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's the official Apple way:</p> <pre><code>// Check for iOS 6 Class mapItemClass = [MKMapItem class]; if (mapItemClass &amp;&amp; [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { // Create an MKMapItem to pass to the Maps app CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(16.775, -3.009); MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]; MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark]; [mapItem setName:@"My Place"]; // Pass the map item to the Maps app [mapItem openInMapsWithLaunchOptions:nil]; } </code></pre> <p>If you want to get driving or walking instructions to the location, you can include a <code>mapItemForCurrentLocation</code> with the <code>MKMapItem</code> in the array in <code>+openMapsWithItems:launchOptions:</code>, and set the launch options appropriately.</p> <pre><code>// Check for iOS 6 Class mapItemClass = [MKMapItem class]; if (mapItemClass &amp;&amp; [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { // Create an MKMapItem to pass to the Maps app CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(16.775, -3.009); MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]; MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark]; [mapItem setName:@"My Place"]; // Set the directions mode to "Walking" // Can use MKLaunchOptionsDirectionsModeDriving instead NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeWalking}; // Get the "Current User Location" MKMapItem MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation]; // Pass the current location and destination map items to the Maps app // Set the direction mode in the launchOptions dictionary [MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] launchOptions:launchOptions]; } </code></pre> <p>You can preserve your original iOS 5 and lower code in an <code>else</code> statement after that <code>if</code>. Note that if you reverse the order of items in the <code>openMapsWithItems:</code> array, you'll get directions from the coordinate <em>to</em> your current location. You could probably use it to get directions between any two locations by passing a constructed <code>MKMapItem</code> instead of the current location map item. I haven't tried that.</p> <p>Finally, if you have an address (as a string) that you want directions to, use the geocoder to create an <code>MKPlacemark</code>, by way of <code>CLPlacemark</code>.</p> <pre><code>// Check for iOS 6 Class mapItemClass = [MKMapItem class]; if (mapItemClass &amp;&amp; [mapItemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString:@"Piccadilly Circus, London, UK" completionHandler:^(NSArray *placemarks, NSError *error) { // Convert the CLPlacemark to an MKPlacemark // Note: There's no error checking for a failed geocode CLPlacemark *geocodedPlacemark = [placemarks objectAtIndex:0]; MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:geocodedPlacemark.location.coordinate addressDictionary:geocodedPlacemark.addressDictionary]; // Create a map item for the geocoded address to pass to Maps app MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark]; [mapItem setName:geocodedPlacemark.name]; // Set the directions mode to "Driving" // Can use MKLaunchOptionsDirectionsModeWalking instead NSDictionary *launchOptions = @{MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving}; // Get the "Current User Location" MKMapItem MKMapItem *currentLocationMapItem = [MKMapItem mapItemForCurrentLocation]; // Pass the current location and destination map items to the Maps app // Set the direction mode in the launchOptions dictionary [MKMapItem openMapsWithItems:@[currentLocationMapItem, mapItem] launchOptions:launchOptions]; }]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload