iOS open link with maps apps

2019-02-20 23:48发布

问题:

I'm working on an event app and I'm trying to add a "Get directions" button that opens up directions in either Apple Maps or Google Maps. I'm happy to use Apple maps for now because it's easy to embed with http://maps.apple.com/?q=XYZ().

My app is displaying an HTML website with a UIWebView, so this may be the problem, but when you press the link it actually opens within the UIWebView and displays Google Maps randomly but accurately. Is there a function I can put into my HTML code that forces the link to open in the native Apple or Google Maps?

回答1:

Here you can find info on the Google Maps URL Scheme

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

Will enforce Google Maps to be used. You might want to do

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

first, to ensure that Google Maps is available. If not, simply call your regular

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

to open apple maps.



回答2:

COMPLETE ANSWER

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

2X

3X



回答3:

Instead of using the http protocol in your URL, try using the maps protocol instead, so that your link looks something like:

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

You can also open it in Google Maps, if the user has it installed, by using a URL such as:

comgooglemaps://?q=Test+Search

Though you might get unexpected results if the user doesn't have it installed.

If this doesn't work, it's possible to use the UIWebView delegate method webView:shouldStartLoadWithRequest:navigationType: to intercept links and open the Maps application properly.