打开具有特定地址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]; } 

和这个 :

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

但button就像它挂钩到什么都没有。 但是这确实有效:

 - (IBAction)openInMaps:(id)sender { NSString *addressString = @"http://maps.apple.com/?q=Cupertino,CA"; NSURL *url = [NSURL URLWithString:addressString]; [[UIApplication sharedApplication] openURL:url]; } 

所以,只要他们是一个空间,这是行不通的。 我怎样才能打开这个地址?

您需要正确地转义URL中的空格:

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

编辑 – 它似乎使用+而不是%20的空间解决了这个问题。

所以为了使它正常工作,你必须使用这个:

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

Swift 2版本格式很好,可以安全地处理optionals,并且不会崩溃你的应用程序:

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

Swift 3版本:

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

这就是我在Objective C中做的事情…我总是先尝试Waze Map,因为说实话,这真是太棒了,我在最后添加了PLIST文件权限:

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

用于播放文件

 <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

2倍

3X

 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)