对于 iOS 6.1.1 及更低版本,使用 UIApplication 的 openURL 方法。它将执行正常的 iPhone 神奇 URL 重新解释。所以
[someUIApplication openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]]
应该调用 Google 地图应用程序。
从 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 地图,请尝试this 代码:
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"]];