Launching the iPhone maps application from your own code
Nov 13th, 2008 by duncan
[Update]: The iPhone 2.2. update seems to have broken url’s in the form: maps://maps.google.com/maps?q= however in my initial testing, it seems if you use http://maps.google.com/maps?q= (i.e. using http:// instead of maps://) then things still seem to function correctly – I’ve updated the post to reflect this.
————
This week I spent some time playing around with the maps application on the iPhone and I thought I’d share some of my findings. (Note that since there is no maps application available in the iPhone simulator all of the below will be launched in Safari instead – you’ll have to actually run it on your iPhone to see it in the maps application.)
In it’s simplest form, to launch the maps application at your last location:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps"]];
which gives you something like this:

Getting slightly more interesting, we can specify the latitude/longitude (“ll“) flag to tell the maps application to launch at a specific location (in this case, my workplace):
NSString *latlong = @"-33.874559,151.219575";
NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@",
[latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
results in:

If you want to get maps to show all the thai restaurants near you, you can specify the query (“q“) flag:
NSString *name = @"thai";
NSString *latlong = @"-33.874559,151.219575";
NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?q=%@&mrt=yp&ll=%@",
[name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
[latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
will give you:

Note that in this example, I’ve include the search mode (“mrt“) flag, with a value of “yp” – this tells maps to search only for businesses – although this is of course not necessary.
Other interesting parameters include the zoom (“z“) flag zoom-level (1-19 defaults is 17 I believe – maps will intelligently alter the zoom level if you specify a query):
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];
zooms out from the previous map:

and the ability to change the map type using the map type (“t”) flag (options are “m”-map, “k”-satellite, “h”- hybrid):
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?t=k"]];
shows the map in satellite mode:

Last, but certainly not least – it’s actually possible to get the maps application to show with your own set of pins dropped on it (rather than using google’s search results as above). In order to do this, you will need to provide a KML file (KML – “Keyhole Markup Language” is a standard for representing geographic annotation – more information can be found here).
This is again done using the query(“q“) flag, but this time you pass in the url of your KML file:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=http://objective-d.com/sample.kml"]];
(in this example, I only have a single entry in my KML file – you can view the file here: http://objective-d.com/sample.kml):

This has just been a taster of the kind of control you have over the maps application, but hopefully it’s got you interested enough to start playing around.
For more information I suggest you check out the handy reference available here: http://mapki.com/index.php?title=Google_Map_Parameters.
If you are interested in including a map from within your iPhone applications, I encourage you to take a look at the iphone-google-maps-component and route-me projects.
Thats Cool Buddy,
I will try to make similar king of thing using MSN maps what say ?
Good article keep it Up .
I can get code from scouring google , still if u can send me it will be good .. dont worry my plans are for msn map not google
Good Luck
Hi Sindhu – not quite sure what you are asking – all of the code examples are inline with the the blog entry – there’s nothing i can send you.
Cheers,
d.
Hi duncan,
I have been looking for this work around for the last couple of days. Your article helped me a lot. In fact, it solved all of my problems.
Thank you very much for sharing this. This is highly appreciated.
Thank You,
Kishore
Hi Duncan,
I have used the URL the way you have given in the sample above. But it is throwing an error “UnSupported URL”, may I know the possible reason for it’s occurrence.
Thanks in advance.
-Kalyan.
2 kalyan
I had similar situation.
Possibly, you get an error “UnSupported URL” because you try to make build (Debug) in the iPhone Simulator – it has no MAPS application installed. You have to check your code on a real iPhone device.
Nice article. But I want more integration with Maps.app: custom overlayed bar and realtime trakc of current location like Navizon do. Any ideas how to implement that?
hardwarrior: Sorry mate, but I don’t believe the stock maps app offers anything close to that level of control – you’ll have to roll your own (I provided links to 2 options at the end of the article).
Cheers.
I’m using your code and I’m able to open Maps. But the problem is that is not accepting any of the parameters and the Maps application always open on the last place that was seeing on the map. I’m using firmware 2.2 and and Ipod Touch. Thanks for any help you might provide.
I’m getting a similar problem to nathanielg. Did something change in 2.2?
It will open at the specified latlong, but the query is never entered into the search bar or executed.
Use http:// instead of maps://
According to the apple docs it will do the right thing, ie if maps is installed it will launch maps, but if not it will use safari.
Neat.
The code works except when passing a url in the query.
like in the example
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"maps://maps.google.com/maps?q=http://objective-d.com/sample.kml"]];
Seems like Google Maps app ignore http:// string
It does look like something must have changed with 2.2 – the ‘q’ parameter doesn’t seem to get passed through, so there’s no way to load KML on top of the map in Maps.
Hi folks,
With regard to the 2.2 update – it does seem to have broken the maps:// format – but if you use http:// instead, then things still seem to be working correctly.
Cheers,
d.
Very cool stuff. I have one related question. Hopefully somebody has an answer.
I saw one app, ManGo, at Apple App Store. It looks like it uses the native Google Maps in iPhone. I’m just wondering how it did that. Does it use openURL to call Google Map? But how it changes the GUI? If it uses Google map tile API to render the map, how did it do it? I heard Google does not open the tile API yet.
Thank you for the article, it was really helpful.
I’ve used the Google-map-Component and developed an application using that but it seems you are using different technique.
could you please let me know how can I use this in my own application in little detail…
such as from where I can start, what kind of application i need to create, touch view or navigation etc..
I really appreciate your help!
This is really interesting, the really powerful one would be being able to pass a URL as the q parameter as you suggest in the post, but a few people (Nick [11] & Alf [12]) have reported this may not work? And the apple sdk rather cryptically implies it won’t work:
http://www.docstoc.com/docs/3595962/iPhone-URL-Scheme
It’s a shame because it would be so cool if it did. Sadly, you need a real phone to test and I won’t be getting one for a couple of months, and Im still waiting for approval from Apple to join their dev program. On the simulator, it does open safari and load the kml from the specified URL, so fingers crossed it is supported; but any further clarification would be really handy. Any way we can fire these questions to Apple?
I’ve just tried passing the URL to a KML file as a parameters to the maps application on the iPod touch, with the latest version of the firmware…I needed to prefix the domain with maps:// (not http://) to get the maps app to open (weird), the KML rendered when it opened the URL in Safari using the http prefix, but didn’t render when opened in the maps app using maps prefix which is a real shame. I’m hoping this works differently on the iPhone? It would be great if they could support this.
Rob
how about launching the other apps like camera, photo, sms, call
im thinking of a web page containing a person’s details like his address, tel #.
the web page will have buttons for maps, camera, photo, sms, call
the user can tap on the maps to know the exact location of the address. similarly he can tap on the camera button if he wants to take a photo of the person. he should also be able to call the person by tapping the call button.
tnx.
Hi nathanielg, Aaron and ofcource Duncan,
It is a really nice post
I had the same issue that I could not pass the query in to maps like the address or latlong anything, however I could solve the problem. I have used the maps as maps://?f=q&hl=en&geocode=&q=1,+Infinite+Loop,+Cupertino,+CA+95014
and that solved the problem.
I have implemented in in the UIWebView…so If you are trying to implement it on Native application, you can follow the code by Duncan.
Thanks again Duncan
Hi,
I am new to iphone app development…I am planning to develop an application(using webview) which should show only the map(not the webpage content of google maps) and i m not intrested in using native app like safari or maps…. Please thro some light…
Try using MapKit (supported in iPhone dev kit – SDK3)
http://blog.objectgraph.com/index.php/2009/04/08/iphone-sdk-30-playing-with-map-kit-part-3/
Hi,
Raul, did you ever figure out how to launch the Camera app using this technique?
/Steve
hi, im pretty new to iphone development can anyone point out where to start with this tutorial, where to add the code etc
any help would be greatly appreciated
Excellent article. I know MapKit framework for iPhone Apps (os 3.0) doesn’t support many things rightnow. But possibly in future release(s).
Thanks. I will try it out.
Hey………..
I want to download this……..Project…….any one help me