11

I am building an app that can open the Maps app with directions from the user's current position to another position. The code looks like this:

- (id)resolveDirectionsFromCoordinate:(CLLocationCoordinate2D)startCoordinate toCoordinate:(CLLocationCoordinate2D)endCoordinate
{
    NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f",
                 startCoordinate.latitude, startCoordinate.longitude,
                 endCoordinate.latitude, endCoordinate.longitude];
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

    return nil;
}

Thos works well in iOS 5.x. In iOS 6, however, this brings up Safari instead, since Maps no longer uses Google Maps.

Does anyone know which URL I should call in iOS 6?

SpaceBeers
  • 13,617
  • 6
  • 47
  • 61
Daniel Saidi
  • 6,079
  • 4
  • 27
  • 29
  • iOS6 is currently still under NDA and you should not ask related questions anywhere but within the Apple developer forum. – Till Jul 17 '12 at 08:56

4 Answers4

19

The Apple Documentation recommends using the equivalent maps.apple.com URL Scheme

so use

http://maps.apple.com/maps?saddr=%f,%f&daddr=%f,%f

instead of

http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f

to be backwards compatible your code would be

    NSString* versionNum = [[UIDevice currentDevice] systemVersion];
    NSString *nativeMapScheme = @"maps.apple.com";
    if ([versionNum compare:@"6.0" options:NSNumericSearch] == NSOrderedAscending){
        nativeMapScheme = @"maps.google.com";
    }
    NSString* url = [NSString stringWithFormat: @"http://%@/maps?saddr=%f,%f&daddr=%f,%f", nativeMapScheme startCoordinate.latitude, startCoordinate.longitude,
                 endCoordinate.latitude, endCoordinate.longitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; 

Alternatively you could also use the maps://saddr=%f,%f&daddr=%f,%fscheme but it does not appear support the full range of parameters.

Resh32
  • 6,500
  • 3
  • 32
  • 40
joneswah
  • 2,788
  • 3
  • 33
  • 32
  • This is wrong, you need to use ll= for latitude and longitude and saddr and daddr for the city names – Charan Sep 30 '12 at 06:14
  • @sreecharan the code above is correct for getting directions from a start and destination location (you can use lat/lon or address). the ll parameter is used when you would like to centre the map at a particular lat/lon – joneswah Oct 02 '12 at 00:17
8

Use + (BOOL)openMapsWithItems:(NSArray *)mapItems launchOptions:(NSDictionary *)launchOptions method. From the MKMapItem Class Reference :

You use this method to pass one or more map items to the Maps app. For example, you might use this method to ask the Maps app to display location-based search results generated by your app. Maps displays pins at each location you specify and uses the contents of each map item object to display additional information.
Andreas Ley
  • 9,109
  • 1
  • 47
  • 57
mspasov
  • 451
  • 6
  • 19
2
// Check for iOS 6
Class mapItemClass = [MKMapItem class];
if (mapItemClass && [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 "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];
   }

That is all :)

0

I recommend checking out CMMapLauncher, a mini-library that I built to launch Apple, Google, and other iOS mapping apps with a specific mapping request. With CMMapLauncher, the code to get the directions in your question would be:

[CMMapLauncher launchMapApp:CMMapAppAppleMaps
          forDirectionsFrom:[CMMapPoint mapPointWithName:@"Origin"
                                              coordinate:startCoordinate]
                         to:[CMMapPoint mapPointWithName:@"Destination"
                                              coordinate:endCoordinate]];

As you can see, it also encapsulates the version checking required between iOS 6 & others.

Joe Trellick
  • 1,125
  • 7
  • 12