Open maps with specific address iOS 7

后端 未结 5 1087
半阙折子戏
半阙折子戏 2021-02-19 04:14

I am trying to make my application open the apple maps application and have the address be pulled up. I tried this :

- (IBAction)openInMaps:(id)sender {
    NSS         


        
相关标签:
5条回答
  • 2021-02-19 04:28

    Swift 3 version:

    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)
        }
    
    0 讨论(0)
  • 2021-02-19 04:30

    This is how I do it in Objective C...I always try the Waze Map first, because to be honest, it's awesome sauce, I added the PLIST file permissions at the end:

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

    FOR PLIST FILE

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

    0 讨论(0)
  • 2021-02-19 04:43

    You need to properly escape the spaces in the URL:

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

    Edit - it seems using + instead of %20 for the spaces solves the problem.

    So to get it to work properly you must use this :

    NSString *addressString = @"http://maps.apple.com/?q=1+Infinite+Loop,+Cupertino,+CA";
    
    0 讨论(0)
  • 2021-02-19 04:51

    Swift 2 version that is formatted nicely, safely handles optionals, and won't crash your app:

    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)
    }
    
    0 讨论(0)
  • 2021-02-19 04:53
    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)
    
    0 讨论(0)
提交回复
热议问题