How to launch Google Maps or Apple Maps from an iOS app

Today Google announced their much-anticipated Google Maps app for iOS. They also plan to make available a Google Maps SDK for iOS allowing iOS developers to integrate Google Maps in their apps, however at present API keys are only available for selected developers.

If you want to provide your app's users with the ability to open a location in Google Maps, there is an option which works today: Google have added a comgooglemaps URL scheme to their app so third-party apps can launch the Google Maps app.

Assuming you'd like to give your users a choice of opening a location in both Apple or Google maps, you can set up some code like this in a new view controller:

  1. #import "ViewController.h"
  2. #import <MapKit/MapKit.h>
  3.  
  4. @implementation ViewController
  5.  
  6. - (void)viewDidLoad
  7. {
  8. [super viewDidLoad];
  9. // Add a button to pop open an action sheet
  10. UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  11. btn.frame = CGRectMake(80,100,160,50);
  12. [btn setTitle:@"Open placemark" forState:UIControlStateNormal];
  13. [btn addTarget:self action:@selector(openActionSheet:) forControlEvents:UIControlEventTouchUpInside];
  14. [self.view addSubview:btn];
  15.  
  16. }
  17. -(void)openActionSheet:(id)sender {
  18. //give the user a choice of Apple or Google Maps
  19. UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Open in Maps" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Apple Maps",@"Google Maps", nil];
  20. [sheet showInView:self.view];
  21. }
  22. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  23. //coordinates for the place we want to display
  24. CLLocationCoordinate2D rdOfficeLocation = CLLocationCoordinate2DMake(31.20691,121.477847);
  25. if (buttonIndex==0) {
  26. //Apple Maps, using the MKMapItem class
  27. MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:rdOfficeLocation addressDictionary:nil];
  28. MKMapItem *item = [[MKMapItem alloc] initWithPlacemark:placemark];
  29. item.name = @"ReignDesign Office";
  30. [item openInMapsWithLaunchOptions:nil];
  31. } else if (buttonIndex==1) {
  32. //Google Maps
  33. //construct a URL using the comgooglemaps schema
  34. NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"comgooglemaps://?center=%f,%f",rdOfficeLocation.latitude,rdOfficeLocation.longitude]];
  35. if (![[UIApplication sharedApplication] canOpenURL:url]) {
  36. NSLog(@"Google Maps app is not installed");
  37. //left as an exercise for the reader: open the Google Maps mobile website instead!
  38. } else {
  39. [[UIApplication sharedApplication] openURL:url];
  40. }
  41. }
  42. }
  43. @end

Here's the app in action, example code is on Github.

    

For more details, see the Google Maps URL Scheme documentation.

Matt Mayer

Matt Mayer is a founder at ReignDesign. Matt is from the UK and was based in Shanghai for ten years. He is now living in Bangkok, Thailand.

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *