Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open maps with specific address iOS 7

I am trying to make my application open the apple maps application and have the address be pulled up. I tried this :

- (IBAction)openInMaps:(id)sender {
    NSString *addressString = @"http://maps.apple.com/?q=1 Infinite Loop, Cupertino, CA";
    NSURL *url = [NSURL URLWithString:addressString];
    [[UIApplication sharedApplication] openURL:url];
}

and this :

- (IBAction)openInMaps:(id)sender {
    NSString *addressString = @"http://maps.apple.com/?q=1_Infinite_Loop,_Cupertino,_CA";
    NSURL *url = [NSURL URLWithString:addressString];
    [[UIApplication sharedApplication] openURL:url];
}

But the button just acts like its hooked to nothing. But this does work :

- (IBAction)openInMaps:(id)sender {
    NSString *addressString = @"http://maps.apple.com/?q=Cupertino,CA";
    NSURL *url = [NSURL URLWithString:addressString];
    [[UIApplication sharedApplication] openURL:url];
}

So, whenever their is a space it doesn't work. How can I open this address?

like image 918
JCode Avatar asked Feb 13 '14 05:02

JCode


1 Answers

You need to properly escape the spaces in the URL:

NSString *addressString = @"http://maps.apple.com/?q=1%20Infinite%20Loop,%20Cupertino,%20CA";

Edit - it seems using + instead of %20 for the spaces solves the problem.

So to get it to work properly you must use this :

NSString *addressString = @"http://maps.apple.com/?q=1+Infinite+Loop,+Cupertino,+CA";
like image 164
rmaddy Avatar answered Nov 01 '22 03:11

rmaddy