2014-02-13 6 views
12

Ich versuche, meine Anwendung öffnen Sie die Apple Maps-Anwendung und lassen Sie die Adresse hochgezogen werden. Ich versuchte dies:Öffnen Sie Karten mit bestimmten Adresse iOS 7

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

und diese:

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

Aber die Taste wirkt wie sein zu nichts verhakt. Aber das funktioniert:

Also, wenn sie ein Leerzeichen ist, funktioniert es nicht. Wie kann ich diese Adresse öffnen?

+1

geben Sie der Dokumentation einen Wirbel https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html –

+0

Danke Mann, wusste nicht einmal, dass es da war ... Ich werde überprüfen es out – TekShock

+0

Ich gehe es: NSString * addressString = [NSString StringWithFormat: @ "http://maps.apple.com/?q=1+Infinite+Loop,+Cupertino,+CA"]; – TekShock

Antwort

26

Sie müssen die Räume in der URL richtig entkommen:

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

bearbeiten - es scheint, mit + statt %20 für die Räume das Problem löst.

Also, um es richtig arbeiten Sie diese verwenden müssen:

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

Es verhält sich immer noch wie der Knopf nichts tut. Allerdings erhalte ich den Fehler: Ungültiger Konvertierungsbezeichner 'I'. Ich weiß was es bedeutet, aber wie reparierst du? – TekShock

0
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) 
8

Swift 2-Version, die gut formatiert ist, Griffe sicher optionals, und wird nicht abstürzen Ihre 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) 
} 
1

Das ist, wie ich es in Objective C mache ... Ich versuche immer zuerst die Waze Map, denn um ehrlich zu sein, es ist eine tolle Soße, ich habe am Ende die PLIST-Dateiberechtigungen hinzugefügt:

- (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){ 

           }]; 
    } 
} 

FÜR PLIST Datei

<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

3

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