Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Pre iOS 6</h2> <p>You need to use Core Location to get the current location, but with that lat/long pair, you can get Maps to route you from there, to a street address or location. Like so:</p> <pre class="lang-objc prettyprint-override"><code>CLLocationCoordinate2D currentLocation = [self getCurrentLocation]; // this uses an address for the destination. can use lat/long, too with %f,%f format NSString* address = @"123 Main St., New York, NY, 10001"; NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&amp;daddr=%@", currentLocation.latitude, currentLocation.longitude, [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; </code></pre> <p>Finally, if you do want to avoid using CoreLocation to explicitly find the current location, and want to use the <code>@"http://maps.google.com/maps?saddr=Current+Location&amp;daddr=%@"</code> url instead, then <a href="http://www.martip.net/blog/localized-current-location-string-for-iphone-apps" rel="noreferrer">see this link that I provided in comments below</a> for how to localize the <strong>Current+Location</strong> string. However, you are taking advantage of another undocumented feature, and as Jason McCreary points out below, it may not work reliably in future releases.</p> <hr> <h2>Update for iOS 6</h2> <p>Originally, <strong>Maps</strong> used Google maps, but now, Apple and Google have separate maps apps. </p> <p><strong>1)</strong> If you wish to route using the Google Maps app, use <a href="https://developers.google.com/maps/documentation/ios/urlscheme#directions" rel="noreferrer">the comgooglemaps URL scheme</a>:</p> <pre class="lang-objc prettyprint-override"><code>NSString* url = [NSString stringWithFormat: @"comgooglemaps://?daddr=%@&amp;directionsmode=driving", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; BOOL opened = [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]]; </code></pre> <p><strong>2)</strong> To use Apple Maps, you can use the new <code>MKMapItem</code> class for iOS 6. <a href="https://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapItem_class/Reference/Reference.html" rel="noreferrer">See the Apple API docs here</a></p> <p>Basically, you will use something like this, if routing to destination <em>coordinates</em> (<code>latlong</code>):</p> <pre class="lang-objc prettyprint-override"><code> MKPlacemark* place = [[MKPlacemark alloc] initWithCoordinate: latlong addressDictionary: nil]; MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark: place]; destination.name = @"Name Here!"; NSArray* items = [[NSArray alloc] initWithObjects: destination, nil]; NSDictionary* options = [[NSDictionary alloc] initWithObjectsAndKeys: MKLaunchOptionsDirectionsModeDriving, MKLaunchOptionsDirectionsModeKey, nil]; [MKMapItem openMapsWithItems: items launchOptions: options]; </code></pre> <p>In order to support both iOS 6+ and pre iOS 6 in the same code, I'd recommend using something like this code that Apple has on the <code>MKMapItem</code> API doc page:</p> <pre class="lang-objc prettyprint-override"><code>Class itemClass = [MKMapItem class]; if (itemClass &amp;&amp; [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { // iOS 6 MKMapItem available } else { // use pre iOS 6 technique } </code></pre> <p>This would assume that your Xcode Base SDK is iOS 6 (or <strong>Latest iOS</strong>).</p>
 

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