在iOS 7上打开特定地址的地图

16

我正在尝试让我的应用程序打开苹果地图应用程序,并拉起地址。我尝试了这个:

- (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];
}

还有这个:

- (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];
}

但按钮好像没有连接到任何东西。但是这样做确实有效:

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

所以,每当有一个空格时,它就无法工作。我该如何打开这个地址?


1
尝试一下文档 https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html - Josh Lowe
谢谢,伙计。我甚至不知道那里有这个... 我会去看看的。 - JCode
我懂了:NSString *addressString = [NSString stringWithFormat:@"http://maps.apple.com/?q=1+Infinite+Loop,+Cupertino,+CA"]; - JCode
不要不必要地使用stringWithFormat:,直接分配字符串即可。 - rmaddy
5个回答

27

您需要正确转义URL中的空格:

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

编辑 - 似乎使用加号+而不是百分号%20来表示空格可以解决问题。

因此,要使其正常工作,您必须使用以下内容:

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

仍然表现得像按钮没有任何作用。但是,我收到了错误消息:无效的转换说明符'I'。我知道它的意思,但如何修复? - JCode

11

Swift 2 版本格式良好,安全地处理可选项,并且不会使您的应用程序崩溃:

let baseUrl: String = "http://maps.apple.com/?q="
let encodedName = "address".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()) ?? ""
let finalUrl = baseUrl + encodedName
if let url = NSURL(string: finalUrl) {
    UIApplication.sharedApplication().openURL(url)
}

4

Swift 3版本:

let baseUrl: String = "http://maps.apple.com/?q="
let encodedName = "yourAddress".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let finalUrl = baseUrl + encodedName
if let url = URL(string: finalUrl)
    {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
    }

4

这是我在Objective C中的做法…… 我总是首先尝试Waze地图,因为说实话,它真的很棒。我在最后添加了PLIST文件权限:

- (IBAction)getDirectionsAction:(id)sender {
    NSURL *googleURL = [[NSURL alloc]
        initWithString:[NSString stringWithFormat:@"comgooglemaps://?daddr=%@", @"44.294349,-70.326973"]];

    NSURL *googleWebURL =
        [[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://www.maps.google.com/maps?daddr=%@",
                                                                 @"44.294349,-70.326973"]];

    NSURL *appleURL = [NSURL URLWithString:@"http://maps.apple.com/?daddr=311+East+Buckfield+Road+Buckfield+Maine"];

    NSURL *wazeURL = [NSURL URLWithString:@"waze://?ll=44.294349,-70.326973&navigate=yes"];

    // Lets try the Waze app first, cuz we like that one the most
    if ([[UIApplication sharedApplication] canOpenURL:wazeURL]) {
        [[UIApplication sharedApplication] openURL:wazeURL
                                           options:@{}
                                 completionHandler:^(BOOL success){
                                 }];
        return;
    }

    // Lets try the Apple Maps app second (great success rate)
    if ([[UIApplication sharedApplication] canOpenURL:appleURL]) {
        [[UIApplication sharedApplication] openURL:appleURL
                                           options:@{}
                                 completionHandler:^(BOOL success){
                                 }];
        return;
    }
    // If those 2 are unsuccessful, let's try the Google Maps app
    if ([[UIApplication sharedApplication] canOpenURL:googleURL]) {
        [[UIApplication sharedApplication] openURL:googleURL
                                           options:@{}
                                 completionHandler:^(BOOL success){
                                 }];
        return;
    }
    // Uh, oh...Well, then lets launch it from the web then.
    else {
        [[UIApplication sharedApplication] openURL:googleWebURL
                                           options:@{}
                                 completionHandler:^(BOOL success){

                                 }];
    }
}

关于 PLIST 文件

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>http://maps.apple.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <true/>
        </dict>
        <key>http://maps.google.com/</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
        </dict>
    </dict>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>waze</string>
    <string>comgooglemaps</string>
</array>
<key>NSLocationAlwaysUsageDescription</key>
<string>For Use for directions</string>

1x

2x

3x


0
Swift 2

 let baseUrl : String = "http://maps.google.com/?q="
            let name : String = tableCellData[indexPath.row]
            let encodedName = "address of yours"
            name.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
            let finalUrl = baseUrl + encodedName!

            let url = NSURL(string: finalUrl)!
            UIApplication.sharedApplication().openURL(url)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接