0

I am trying to launch a map from within my app, and I am using the following:

- (void) showMap:(id) button {
    UIApplication *application = [UIApplication sharedApplication];
    NSString *address = [((PhoneButton *) button).cell.client fullAddress];
    NSString *addressUrl = [NSString stringWithFormat: @"http://maps.apple.com/?q=%@", address];
    NSLog(@"Maps String: %@", addressUrl);
    NSURL *map_url = [NSURL URLWithString:addressUrl];
    [application openURL:map_url];
};

Well, it is not working. I tried to find the issue but looks like I am doing it right. So, what am I missing?

PS: My address format is like "800, Madison Ave, New York, NY"

marcelorocks
  • 2,005
  • 3
  • 18
  • 28
  • Duplicate of how to open Maps in iOS 6 programatically: http://stackoverflow.com/q/12504294/558933. This question also has the answer. – Robotic Cat Feb 24 '13 at 20:21

2 Answers2

2

Try this.

Your address format is like

"800, Madison Ave, New York, NY"

which contain space between words. Remove space with "+" sign. your final URL should be like below URL. with this you can launch Map from app on iOS 6.

http://maps.apple.com/?q=800,+Madison+Ave,+New+York,+NY

Wolverine
  • 4,264
  • 1
  • 27
  • 49
0

URLs can't have spaces. Spaces and other special characters need to be escaped properly. You want:

NSString *escapedAddress = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *addressUrl = [NSString stringWithFormat: @"http://maps.apple.com/?q=%@", escapedAddress];
rmaddy
  • 314,917
  • 42
  • 532
  • 579