iOS地图应用

时间:2015-08-31 15:44:29

标签: html ios google-maps maps

我正在制作一个活动应用,而且我正在尝试添加一个"获取路线"在Apple Maps或Google Maps中打开方向的按钮。我现在很高兴使用Apple地图,因为它很容易嵌入http://maps.apple.com/?q=XYZ()

我的应用正在显示一个带有UIWebView的HTML网站,因此这可能是问题所在,但是当您按下链接时,它实际上会在UIWebView内打开并随机但准确地显示Google地图。我可以在HTML代码中添加一个强制链接在原生Apple或Google Maps中打开的函数吗?

3 个答案:

答案 0 :(得分:2)

完整答案

CODE:

- (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 PROPERTIES:

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

<强> BUTTON

1x 1X

2x 2X

3x 3X

答案 1 :(得分:1)

请尝试使用http协议,而不是在您的网址中使用maps协议,以便您的链接如下所示:

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

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

comgooglemaps://?q=Test+Search

如果用户没有安装,可能会出现意外结果。

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

答案 2 :(得分:0)

您可以在此处找到有关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() 

打开苹果地图。