Home » Ios » How can I launch the Google Maps iPhone application from within my own native application?

How can I launch the Google Maps iPhone application from within my own native application?

Posted by: admin November 30, 2017 Leave a comment

Questions:

The Apple Developer Documentation (link is dead now) explains that if you place a link in a web page and then click it whilst using Mobile Safari on the iPhone, the Google Maps application that is provided as standard with the iPhone will launch.

How can I launch the same Google Maps application with a specific address from within my own native iPhone application (i.e. not a web page through Mobile Safari) in the same way that tapping an address in Contacts launches the map?

NOTE: THIS ONLY WORKS ON THE DEVICE ITSELF. NOT IN THE SIMULATOR.

Answers:

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).

Questions:
Answers:

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.

Questions:
Answers:

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“]];

Questions:
Answers:

There is also now the App Store Google Maps app, documented at https://developers.google.com/maps/documentation/ios/urlscheme

So you’d first check that it’s installed:

[[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:@"comgooglemaps://"]];

And then you can conditionally replace http://maps.google.com/maps?q= with comgooglemaps://?q=.

Questions:
Answers:

Here’s the Apple URL Scheme Reference for Map Links: http://developer.apple.com/iphone/library/featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html

The rules for creating a valid map link are as follows:

  • The domain must be google.com and the subdomain must be maps or ditu.
  • The path must be /, /maps, /local, or /m if the query contains site as the key and local as the value.
  • The path cannot be /maps/*.
  • All parameters must be supported. See Table 1 for list of supported parameters**.
  • A parameter cannot be q=* if the value is a URL (so KML is not picked up).
  • The parameters cannot include view=text or dirflg=r.

**See the link above for the list of supported parameters.

Questions:
Answers:

If you are using ios 10 then please don’t forget to add Query Schemes in Info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>comgooglemaps</string>
</array>

If you are using objective-c

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps:"]]) {
    NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?ll=%@,%@",destinationLatitude,destinationLongitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    } else { 
    NSString *string = [NSString stringWithFormat:@"http://maps.google.com/maps?ll=%@,%@",destinationLatitude,destinationLongitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
    }

If you are using swift 2.2

if UIApplication.sharedApplication().canOpenURL(NSURL(string: "comgooglemaps:")!) {
    var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.sharedApplication().openURL(NSURL(string: urlString)!)
}
else {
    var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.sharedApplication().openURL(NSURL(string: string)!)
}

If you are using swift 3.0

if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps:")!) {
    var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.shared.openURL(URL(string: urlString)!)
}
else {
    var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.shared.openURL(URL(string: string)!)
}

Questions:
Answers:

For the phone question, are you testing on the simulator? This only works on the device itself.

Also, openURL returns a bool, which you can use to check if the device you’re running on supports the functionality. For example, you can’t make calls on an iPod Touch 🙂

Questions:
Answers:

“g” change to “q”

[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]]

Questions:
Answers:

If you’re still having trouble, this video shows how to get “My maps” from google to show up on the iphone — you can then take the link and send it to anybody and it works.

http://www.youtube.com/watch?v=Xo5tPjsFBX4

Questions:
Answers:

For move to Google map use this api and send destination latitude and longitude

NSString* addr = nil;
     addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=Posizione attuale", destinationLat,destinationLong];

NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];

Questions:
Answers:

If you need more flexabilty than the Google URL format gives you or you would like to embed a map in your application instead of launching the map app there is an example found at https://sourceforge.net/projects/quickconnect.

Questions:
Answers:

If you need more flexibility than the Google URL format gives you or you would like to embed a map in your application instead of launching the map app here is an example.

It will even supply you with the source code to do all of the embedding.

Questions:
Answers:

iPhone4 iOS 6.0.1 (10A523)

For both Safari & Chrome.
Both latest version up till now (2013-Jun-10th).

The URL scheme below also work.
But in case of Chrome only works inside the page doesn’t work from the address bar.

maps:[email protected],longtitude

Questions:
Answers:
**Getting Directions between 2 locations**

        NSString *googleMapUrlString = [NSString stringWithFormat:@"http://maps.google.com/?saddr=%@,%@&daddr=%@,%@", @"30.7046", @"76.7179", @"30.4414", @"76.1617"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapUrlString]];