Friday, February 27, 2009

Mapping Directions from your App

If you want to give your user driving directions by launching the Maps application, it's actually pretty easy. You just create a regular Google Maps URL and use UIApplication to open it up; Your phone will recognize that the URL is for directions and will open Maps.app rather than Safari.app automatically.

The URL format is very easy for directions:

http://maps.google.com/maps?saddr=[source address or coordinates]&daddr=[destination address or coordinates]

Here is an example of taking the coordinates pulled from Core Location, and using that as the starting point for directions:

    NSString *destinationString = @"Cupertino,California";
NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=%f,%f&daddr=%@", newLocation.coordinate.latitude, newLocation.coordinate.longitude, destinationString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

Of course, this doing this quits your application and launches Maps.app, so make sure you've got everything saved before you do it.

There is one gotcha if you're using search terms or a physical address instead of coordinates. Maps.app doesn't like spaces. Unlike Safari, Maps.app won't automatically convert spaces to %20 for you. On the other hand, you don't want to use NSString's stringByAddingPercentEscapesUsingEncoding: either, because that method will convert commas, dashes, and certain other characters, which may cause problems when launching Maps.app (and probably will - those commas are important!).

In most cases, you can just remove the spaces by doing this:

    NSString *newAddressPartOfURL = [addressPartOfURL stringByReplacingOccurrencesOfString:@" " withString:@""]

Technically speaking, we're cheating. We should be able to URL-encode the whole address no problem, and it should work. But… it doesn't. This is one of those situations where you need to cheat a little to get things to work properly. I can tell you from first-hand experience that if you URL-encode those commas in your address, you will get a message that Maps.app couldn't find the specified address.



4 comments:

mare said...

CFURLCreateStringByAddingPercentEscapes has an option to omit certain characters from encoding. That might be a less cheating. It can be used with NSStrings.

Here is an example straight from my code for a link i an email to the iTunes store:

NSString *appID = @"293622437"; //This is not an actual appID
NSString *urlStringBase = [@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=" stringByAppendingString:appID];

NSString *encodedURL = (NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)urlStringBase, NULL, CFSTR("&"), kCFStringEncodingUTF8);

Jeff LaMarche said...

mare:

I'm not sure if it's less cheating - any app that accepts URLs should be able to handle url encoding of any characters, but it's definitely a good, more flexible alternative. Thanks for posting!

Edwin said...

scrub m65 kamagra attorney lawyer body scrub field jacket lovegra marijuana attorney injury lawyer

h4ns said...

What youre saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I also love the images you put in here. They fit so well with what youre trying to say. Im sure youll reach so many people with what youve got to say.

Arsenal vs Huddersfield Town live streaming
Arsenal vs Huddersfield Town live streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Wolverhampton Wanderers vs Stoke City Live Streaming
Notts County vs Manchester City Live Streaming
Notts County vs Manchester City Live Streaming
Bologna vs AS Roma Live Streaming
Bologna vs AS Roma Live Streaming
Juventus vs Udinese Live Streaming
Juventus vs Udinese Live Streaming
Napoli vs Sampdoria Live Streaming
Napoli vs Sampdoria Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
Fulham vs Tottenham Hotspur Live Streaming
AS Monaco vs Marseille Live Streaming
AS Monaco vs Marseille Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Alajuelense vs Perez Zeledon Live Streaming
Technology News | News Today | Live Streaming TV Channels