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,%f
scheme 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:
- For iOS 6 maps - You can set
MKLaunchOptionsDirectionsModeWalking
instead ofMKLaunchOptionsDirectionsModeDriving
- 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
Find Attributes from Attributed String That User Typed
Swift: Retrieving Text from a Uitextfield in a Custom Uitableviewcell and Putting It in an Array
Will iOS Region Monitoring Call Didenterregion on My App If My App Is in a Terminated State
Error: Uicollectionview Must Be Initialized with a Non-Nil Layout Parameter
How to Do a Native "Pulse Effect" Animation on a Uibutton - iOS
Bundle.Main.Path(Forresource:Oftype:Indirectory:) Returns Nil
Transition Animation Not Working in Swiftui
How to Properly Order Data from Firebase Chronologically
Skspritenode - Create a Round Corner Node
Uitextfield -Webview No Longer Supported
iOS Automatically Add Hyphen in Text Field
Swift Override Function in Extension
Running Xcodebuild from a Forked Terminal
Alamofire Download in Background Session
Push Notifications Not Being Received on iOS 10, But Working on iOS 9 and Before