iOS 使用地图应用打开 link

iOS open link with maps apps

我正在开发一个活动应用程序,我正在尝试添加一个 "Get directions" 按钮,用于在 Apple 地图或 Google 地图中打开路线。我很高兴现在使用 Apple 地图,因为它很容易嵌入 http://maps.apple.com/?q=XYZ()

我的应用程序显示 HTML 网站,其中包含 UIWebView,所以这可能是问题所在,但当您按下 link 时,它实际上在 UIWebView 内打开] 并随机但准确地显示 Google 地图。是否有一个函数可以放入我的 HTML 代码中,强制 link 在本机 Apple 或 Google 地图中打开?

不要在 URL 中使用 http 协议,而是尝试使用 maps 协议,这样您的 link 看起来像:

maps://maps.apple.com/?q=Test+Search

您也可以在 Google 地图中打开它,如果用户安装了它,使用 URL 例如:

comgooglemaps://?q=Test+Search

如果用户没有安装它,您可能会得到意想不到的结果。

如果这不起作用,可以使用 UIWebView 委托方法 webView:shouldStartLoadWithRequest:navigationType: 拦截 links 并正确打开地图应用程序。

在这里您可以找到有关 Google Maps URL Scheme

的信息
UIApplication.sharedApplication().openURL(NSURL(string:
  "comgooglemaps://?q=XYZ")!)

将强制使用 Google 地图。 你可能想做

UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!)

首先,确保 Google 地图可用。如果没有,只需致电您的常规

http://maps.apple.com/?q=XYZ() 

打开苹果地图。

完整答案

代码:

- (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
    if ([[UIApplication sharedApplication] canOpenURL:appleURL]) {
        [[UIApplication sharedApplication] openURL:appleURL
                                           options:@{}
                                 completionHandler:^(BOOL success){
                                 }];
        return;
    }
    // If Apple Maps is 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