Apple 开发人员文档解释说,如果您在网页中放置一个链接,然后在 iPhone 上使用 Mobile Safari 时单击它,则作为 iPhone 标准提供的 Google 地图应用程序将启动。
如何从我自己的本地 iPhone 应用程序(即不是通过 Mobile Safari 的网页)中启动具有特定地址的相同 Google 地图应用程序,就像在联系人中点击地址启动地图一样?
Apple 开发人员文档解释说,如果您在网页中放置一个链接,然后在 iPhone 上使用 Mobile Safari 时单击它,则作为 iPhone 标准提供的 Google 地图应用程序将启动。
如何从我自己的本地 iPhone 应用程序(即不是通过 Mobile Safari 的网页)中启动具有特定地址的相同 Google 地图应用程序,就像在联系人中点击地址启动地图一样?
对于 iOS 6.1.1 及更低版本,请使用 UIApplication 的 openURL 方法。它将执行正常的 iPhone 神奇 URL 重新解释。所以
[someUIApplication openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]]
应该调用谷歌地图应用程序。
从 iOS 6 开始,您将调用 Apple 自己的地图应用程序。为此,使用您要显示的位置配置一个 MKMapItem 对象,然后向其发送 openInMapsWithLaunchOptions 消息。要从当前位置开始,请尝试:
[[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];
为此,您需要与 MapKit 链接(我相信它会提示您进行位置访问)。
你也可以使用
UIApplication *app = [UIApplication sharedApplication];
[app openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]];
要在特定坐标处打开 Google 地图,请尝试以下代码:
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]];
您可以将 latlong 字符串替换为 CoreLocation 中的当前位置。
您还可以使用 (“z”) 标志指定缩放级别。值为 1-19。这是一个例子:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];