当我按下应用程序中的button时,如何打开Goog​​le地图?

我有这个应用程序,我想使用谷歌地图或苹果地图打开时,用户在我的应用程序中按下button。 我将如何能够做到这一点? 有没有像打开应用程序的地图链接,还是其他的东西? 如果你能把我指向正确的方向,这将是非常有帮助的。 谢谢! 我有这样的button设置如下:

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.... } 

用这个:

 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) } } 

你可以在这里find更多有关google maps URL scheme的信息

编辑:你必须添加一个关键到你的info.plist这个工作。

 <key>LSApplicationQueriesSchemes</key> <array> <string>googlechromes</string> <string>comgooglemaps</string> </array> 

编辑:每更新的Google地图文档添加“googlechromes”以上plist。

在button点击(在行动中)用以下代码调用函数“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) } } 

编辑LSApplicationQueriesSchemes的info.plist:

在这里输入图像说明

希望会有所帮助!