Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening maps with current location and directions in iOS 6

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?

like image 611
Daniel Saidi Avatar asked Jul 16 '12 20:07

Daniel Saidi


People also ask

How do I get directions on my iPhone 6?

Get directions for driving Do one of the following: Say something like “Hey Siri, give me driving directions home.” Learn how to ask Siri. 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.

Why wont my Maps show directions?

You may need to update your Google Maps app, connect to a stronger Wi-Fi signal, recalibrate the app, or check your location services. You can also reinstall the Google Maps app if it isn't working, or simply restart your iPhone or Android phone.


1 Answers

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.

like image 172
joneswah Avatar answered Sep 21 '22 11:09

joneswah