如何从自己的本机应用程序中启动Google Maps iPhone应用程序?
Apple开发人员文档(现在链接已失效)说明,如果我们在iPhone上使用Mobile Safari时将链接放在网页中然后单击它,则会启动iPhone随附提供的Google Maps应用程序。
如何在我自己的本机iPhone应用程序(即不是通过Mobile Safari的网页)中使用特定地址启动相同的Google Maps应用程序,就像在"通讯录"中轻按地址即可启动地图一样?
注意:这仅适用于设备本身。不在模拟器中。
解决方案
回答
对于iOS 5.1.1及更低版本,请使用UIApplication的openURL方法。它将执行普通的iPhone神奇URL重新解释。所以
[someUIApplication openURL:[NSURL URLWithString:@"http://maps.google.com/maps?q=London"]]
应该调用Google地图应用。
从iOS 6开始,我们将调用Apple自己的Maps应用。为此,用我们要显示的位置配置一个" MKMapItem"对象,然后向其发送" openInMapsWithLaunchOptions"消息。要从当前位置开始,请尝试:
[[MKMapItem mapItemForCurrentLocation] openInMapsWithLaunchOptions:nil];
为此,我们需要针对MapKit进行链接(我相信它会提示我们进行位置访问)。
回答
确切地。我们需要实现的代码是这样的:
UIApplication *app = [UIApplication sharedApplication]; [app openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]];
因为根据文档,UIApplication仅在Application Delegate中可用,除非我们调用sharedApplication。
回答
如果我们需要的灵活性超出了Google URL格式所提供的灵活性,或者我们想在应用程序中嵌入地图而不是启动地图应用程序,请参考以下示例。
它甚至会为我们提供执行所有嵌入的源代码。
回答
要在特定坐标处打开Google Maps,请尝试以下代码:
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]];
我们可以使用CoreLocation中的当前位置替换latlong字符串。
我们也可以使用(z)标志指定缩放级别。值是1-19. 这是一个例子:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.google.com/maps?z=8"]];
回答
对于电话问题,我们是否正在模拟器上进行测试?这仅适用于设备本身。
另外,openURL返回一个布尔值,我们可以使用该布尔值来检查正在运行的设备是否支持该功能。例如,我们不能在iPod Touch上拨打电话:-)
回答
这是用于地图链接的Apple URL方案参考:http://developer.apple.com/iphone/library/featuredarticles/iPhoneURLScheme_Reference/Articles/MapLinks.html
The rules for creating a valid map link are as follows: The domain must be google.com and the subdomain must be maps or ditu. The path must be /, /maps, /local, or /m if the query contains site as the key and local as the value. The path cannot be /maps/*. All parameters must be supported. See Table 1 for list of supported parameters**. A parameter cannot be q=* if the value is a URL (so KML is not picked up). The parameters cannot include view=text or dirflg=r.
**请参阅上面的链接以获取受支持参数的列表。
回答
将" g"更改为" q"
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://maps.google.com/maps?q=London"]]
回答
如果我们仍然遇到问题,该视频将演示如何从Google获取"我的地图"以在iphone上显示-然后我们可以获取链接并将其发送给任何人,并且可以正常运行。
http://www.youtube.com/watch?v=Xo5tPjsFBX4