How to Invoke iPhone Maps for Directions with Current Location as Start Address

Apple maps route request URL from current location

You were pretty close!

Apple Maps:

http://maps.apple.com/maps?saddr=Current%20Location&daddr=<Your Location>

Google Maps:

comgooglemaps-x-callback://?saddr=&daddr=<Your Location>

Opening maps with current location and directions in iOS 6

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.

Open Maps app from Code - Where/How to find the Current Location?

You don't have to determine the user's current location yourself, the Maps app will take care of it.

Instead of passing a latitude/longitude pair you can pass Current%%20Location and Maps will determine the user's current location itself.

%20 is a url-encoded space character, and the extra % escapes the actual % so it won't be interpreted as a format substitution.


Thanks to @Carlos P for pointing out my escape character blunder in the original answer.

iOS Google Maps Question

I believe that saddr=Current%20Location&daddr=... is what you want.

Opening Maps App with Current Location & Directions in iOS 5

Make sure you're escaping the the NSString using stringByAddingPercentEscapesUsingEncoding

NSString *url = [NSString stringWithFormat: @"http://maps.google.com/mapssaddr=Current+Location&daddr=%@ %@ %@ %@", streetString , cityString, stateString, zipString];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];

Call Maps for directions from inside your app - iOS5 iOS6

I had a similar problem and I had to create some conditional OS code to deal with the fact that the Google Maps application has been removed. From the new MKMapItem Reference

//first create latitude longitude object
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude,longitude);

//create MKMapItem out of coordinates
MKPlacemark* placeMark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark:placeMark];

if([destination respondsToSelector:@selector(openInMapsWithLaunchOptions:)])
{
//using iOS6 native maps app
[destination openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}];
}
else
{
//using iOS 5 which has the Google Maps application
NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=Current+Location&daddr=%f,%f", latitude, longitude];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}

[placeMark release];
[destination release];

To get walking directions:

  1. For iOS 6 maps - You can set MKLaunchOptionsDirectionsModeWalking instead of MKLaunchOptionsDirectionsModeDriving
  2. For Google maps - Add &dirflg=w to the url.

I think it's better to use the openInMapsWithLaunchOptions in iOS6 because it gives you complete control over how the maps application will respond.



Related Topics



Leave a reply



Submit