Sponsored Links

Archives

Howto Launch iPhone Maps from within your code

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://http://www.macoscoders.com/images/sample.kml"]];

(in this example, I only have a single entry in my KML file – you can view the file here: http://www.macoscoders.com/images/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.

[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

Post to Twitter

1 comment to Howto Launch iPhone Maps from within your code

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by WP Hashcash