views:

27077

answers:

10

The Apple Developer Documentation 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.

+22  A: 

I believe UIApplication has an openURL method, that will perform the normal iPhone magical URL reinterpretation. so

[someUIApplication openURL:[[NSURL alloc] initWithString: @"http://maps.google.com/maps?q=London"]]

should invoke the Google maps app.

Edit: Apple NDA is long, long gone.

Adam Wright
The NDA is lifted for published code.
Giao
Wiki with description of params: http://mapki.com/wiki/Google_Map_ParametersApple iphone os programming guide Appendix A has a list of supported parameters on the iphone.
Carl Coryell-Martin
That should be q= not g=.
Steven Fisher
+14  A: 

Exactly. The code that you need to achieve this is:

UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString: @"http://maps.google.com/maps?g=London"]];

since as per the documentation, UIApplication is only available in the Application Delegate unless you call sharedApplication.

DavidM
You're leaking the NSURL.I would recommend:[app openURL:[NSURL URLWithString:@"http://maps.google.com/maps?g=London"]]so that it is autoreleased.
Matthew McGoogan
A: 

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.

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

Lee
+7  A: 

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

Jane Sales
+2  A: 

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

Jane Sales
+7  A: 

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.

harrylove
A: 

"g" change to "q"

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

Hey, been googling for this a while... just found this.. dunno if it'll work.. will try it out now..

http://macmost.com/iphone-gps-location-with-javascript.html

A: 

http://maps.google.com/maps?q=London

bindegal
A: 

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

Tex