How can I launch the Google Maps iPhone application from within my own native application?
For iOS 5.1.1 and lower, use the openURL
method of UIApplication
. It will perform the normal iPhone magical URL reinterpretation. so
[someUIApplication openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]]
should invoke the Google maps app.
From iOS 6, you'll be invoking Apple's own Maps app. For this, configure an MKMapItem
object with the location you want to display, and then send it the openInMapsWithLaunchOptions
message. To start at the current location, try:
[[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];
You'll need to be linked against MapKit for this (and it will prompt for location access, I believe).
Exactly. The code that you need to achieve this is something like that:
UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]];
since as per the documentation, UIApplication is only available in the Application Delegate unless you call sharedApplication.
To open Google Maps at specific co-ordinates, try this code:
NSString *latlong = @"-56.568545,1.256281";
NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@",
[latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
You can replace the latlong string with the current location from CoreLocation.
You can also specify the zoom level, using the (”z“) flag. Values are 1-19. Here's an example:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];