【问题标题】:Open maps with specific address iOS 7打开带有特定地址 iOS 7 的地图
【发布时间】:2014-03-11 20:48:30
【问题描述】:

我正在尝试让我的应用程序打开苹果地图应用程序并提取地址。我试过这个:

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

因此,只要它们是空间,它就不起作用。我怎样才能打开这个地址?

【问题讨论】:

标签: ios iphone map street-address apple-maps


【解决方案1】:

您需要正确转义 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”。我知道这意味着什么,但你如何解决?
【解决方案2】:

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)
}

【讨论】:

    【解决方案3】:

    这就是我在 Objective C 中的做法......我总是先尝试 Waze Map,因为老实说,它很棒,我在最后添加了 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>
    

    【讨论】:

      【解决方案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)
          }
      

      【讨论】:

        【解决方案5】:
        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)
        

        【讨论】:

          猜你喜欢
          • 2016-01-05
          • 2015-02-09
          • 2017-10-10
          • 2013-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-16
          • 2015-10-27
          相关资源
          最近更新 更多