如何从我自己的原生应用程序中启动Google Maps iPhone应用程序?

Dav*_*idM 68 google-maps objective-c ios

苹果开发者文档(链接现在是死的)解释说,如果你把在网页中的链接,然后单击它,而在iPhone上,也作为标准配置提供与iPhone将推出谷歌地图应用程序中使用移动Safari浏览器.

如何在我自己的原生iPhone应用程序中启动具有特定地址的相同Google地图应用程序(即通过Mobile Safari不是网页),就像在联系人中点击地址启动地图一样?

注意:这仅适用于设备本身.不在模拟器中.

Ada*_*ght 65

对于iOS 5.1.1及更低版本,请使用openURL方法UIApplication.它将执行正常的iPhone魔法URL重新解释.所以

[someUIApplication openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]]
Run Code Online (Sandbox Code Playgroud)

应该调用Google地图应用.

从iOS 6开始,您将调用Apple自己的地图应用程序.为此,MKMapItem请使用要显示的位置配置对象,然后将其发送给openInMapsWithLaunchOptions消息.要从当前位置开始,请尝试:

[[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];
Run Code Online (Sandbox Code Playgroud)

您需要将此链接与MapKit相关联(我相信它会提示进行位置访问).

  • 维基描述符号:http://mapki.com/wiki/Google_Map_Parameters Apple iphone os编程指南附录A列出了iphone支持的参数. (2认同)
  • 那应该是q = not g =. (2认同)
  • 根据Apple的文档(http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html),您应该使用maps.apple.com,如:http://maps.apple.com /?q =伦敦这将避免根据操作系统版本输入不同的代码,因为在iOS 5中,它将重定向到谷歌地图. (2认同)

Dav*_*idM 31

究竟.您需要实现此目的的代码是这样的:

UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]];
Run Code Online (Sandbox Code Playgroud)

因为根据文档,除非您调用sharedApplication,否则UIApplication仅在Application Delegate中可用.

  • 你正在泄漏NSURL.我建议:[app openURL:[NSURL URLWithString:@"http://maps.google.com/maps?g=London"]]以便自动释放. (7认同)

Jan*_*les 29

要在特定坐标下打开Goog​​le地图,请尝试以下代码:

NSString *latlong = @"-56.568545,1.256281";
NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@",
[latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
Run Code Online (Sandbox Code Playgroud)

您可以使用CoreLocation中的当前位置替换latlong字符串.

您还可以使用("z")标志指定缩放级别.值为1-19.这是一个例子:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@" http://maps.google.com/maps?z=8 "]];

  • 不知何故,对于"ll"我不能正常工作,我必须使用"q"代替.例如,http://maps.google.com/maps?q = 48.8588054,2.3030451和http://maps.google.com/maps?ll=48.8588054,2.3030451不指向同一地址. (2认同)

Mic*_*aks 18

现在还有App Store Google Maps应用程序,记录在https://developers.google.com/maps/documentation/ios/urlscheme

所以你首先检查它是否已安装:

[[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps://"]];

然后你就可以有条件地替换http://maps.google.com/maps?q=comgooglemaps://?q=.


Har*_*ove 13

以下是地图链接的Apple URL方案参考:http://developer.apple.com/iphone/library/featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html

创建有效地图链接的规则如下:

  • 域必须是google.com,子域必须是maps或ditu.
  • 如果查询包含site作为键,local作为值,则路径必须是/,/ maps,/ local或/ m.
  • 路径不能是/ maps/*.
  • 必须支持所有参数.有关支持的参数列表**,请参阅表1.
  • 如果值是URL(因此未拾取KML),则参数不能为q =*.
  • 参数不能包含view = text或dirflg = r.

**有关支持的参数列表,请参阅上面的链接.


Ruc*_*mal 9

如果您使用的是ios 10,请不要忘记在Info.plist中添加查询方案

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>comgooglemaps</string>
</array>
Run Code Online (Sandbox Code Playgroud)

如果您使用objective-c

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"comgooglemaps:"]]) {
    NSString *urlString = [NSString stringWithFormat:@"comgooglemaps://?ll=%@,%@",destinationLatitude,destinationLongitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    } else { 
    NSString *string = [NSString stringWithFormat:@"http://maps.google.com/maps?ll=%@,%@",destinationLatitude,destinationLongitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
    }
Run Code Online (Sandbox Code Playgroud)

如果您使用的是swift 2.2

if UIApplication.sharedApplication().canOpenURL(NSURL(string: "comgooglemaps:")!) {
    var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.sharedApplication().openURL(NSURL(string: urlString)!)
}
else {
    var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.sharedApplication().openURL(NSURL(string: string)!)
}
Run Code Online (Sandbox Code Playgroud)

如果您使用的是swift 3.0

if UIApplication.shared.canOpenURL(URL(string: "comgooglemaps:")!) {
    var urlString = "comgooglemaps://?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.shared.openURL(URL(string: urlString)!)
}
else {
    var string = "http://maps.google.com/maps?ll=\(destinationLatitude),\(destinationLongitude)"
    UIApplication.shared.openURL(URL(string: string)!)
}
Run Code Online (Sandbox Code Playgroud)