xcode 当我按下应用程序中的按钮时,如何打开谷歌地图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32772727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How would I be able to open google maps when I press a button in my app?
提问by coding22
I have this app and I want to use google maps or apple maps to open when the user presses a button in my app. How would I be able to do this? Is there like a link to the maps that opens the app or is it something else? If you can point me in the right direction it would be really helpful. Thanks! I have the button set up below like this:
我有这个应用程序,我想在用户按下我的应用程序中的按钮时使用谷歌地图或苹果地图打开。我怎么能做到这一点?是否有打开应用程序的地图链接或其他内容?如果您能指出我正确的方向,那将非常有帮助。谢谢!我在下面设置了这样的按钮:
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in (touches ) {
let location = touch.locationInNode(self)
let node = self.nodeAtPoint(location)
if node.name == "openMaps" {
//code to open google maps or apple maps....
}
回答by Sman25
Use this:
用这个:
if node.name == "openMaps" {
let customURL = "comgooglemaps://"
if UIApplication.sharedApplication().canOpenURL(NSURL(string: customURL)) {
UIApplication.sharedApplication().openURL(NSURL(string: customURL))
}
else {
var alert = UIAlertController(title: "Error", message: "Google maps not installed", preferredStyle: UIAlertControllerStyle.Alert)
var ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)
alert.addAction(ok)
self.presentViewController(alert, animated:true, completion: nil)
}
}
You can find more info about the google maps URL scheme here
您可以在此处找到有关谷歌地图 URL 方案的更多信息
Edit: You must add a key to your info.plist
for this to work.
编辑:您必须info.plist
为此添加一个密钥才能工作。
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>
Edit: Per updated Google Maps docsadded "googlechromes" to plist above also.
编辑:每个更新的谷歌地图文档也将“googlechromes”添加到上面的 plist 中。
回答by JaspreetKour
At button click (in action) call function "directions()" with following code :
在按钮单击(在操作中)调用函数“directions()”,代码如下:
func directions() {
// if GoogleMap installed
if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
UIApplication.shared.openURL(NSURL(string:
"comgooglemaps://?saddr=&daddr=\(Float(event.addressLat)!),\(Float(event.addressLong)!)&directionsmode=driving")! as URL)
} else {
// if GoogleMap App is not installed
UIApplication.shared.openURL(NSURL(string:
"https://maps.google.com/?q=@\(Float(event.addressLat)!),\(Float(event.addressLong)!)")! as URL)
}
}
Edit your info.plist for LSApplicationQueriesSchemes :
为 LSApplicationQueriesSchemes 编辑您的 info.plist :
Hope will help!
希望会有所帮助!