Posts Tagged ‘Maps’

iPhone SDK: Show address in Google Maps

February 17, 2009

As a part of the project i am currently working on (seems there is a difference between finished and FINISHED), i had to implement the pretty standard feature of displaying an address in Google Maps. To my suprise it took me quite some time to find the code, so to make things easier for others, i thought i would post it here.

If found a demo app (http://www.appsamuck.com/day3.html) that shows how to use multiple different url types (phone, email, etc.). At a glance it worked fine, but when i tried to open a Danish address with special characters (æ, ø, å), the “address” bar in Maps would just read “null”. It turned out that who ever put together the sample app, used NSASCIIStringEncoding as the format for encoding the spaces in the address – in my example i changed it to NSUTF8StringEncoding. Enjoy:

// Where is Apple in Denmark on the map anyway?
NSString* addressText = @"Nørre Voldgade 11, 1358 København K";
// URL encode the spaces
addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
// Combine url
NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];
// Open url
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];

Advertisements