当我在应用程序中按下按钮时,如何才能打开谷歌地图?

cod*_*g22 14 xcode google-maps ios apple-maps swift

我有这个应用程序,当用户按下我的应用程序中的按钮时,我想使用谷歌地图或苹果地图打开.我怎么能这样做?有没有像打开应用程序的地图的链接或其他东西?如果你能指出我正确的方向,那将是非常有帮助的.谢谢!我按下这个按钮设置如下:

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

    }
Run Code Online (Sandbox Code Playgroud)

小智 60

用这个:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到有关Google地图网址方案的更多信息

编辑:您必须添加一个密钥才能info.plist使其正常工作.

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>googlechromes</string>
    <string>comgooglemaps</string>
</array>
Run Code Online (Sandbox Code Playgroud)

编辑:每个更新的谷歌地图文档也将"googlechromes"添加到plist上面.

  • 这不适用于iOS9.您可能会收到此警告:"此应用不允许查询计划comgooglemaps".您还需要列出您的方案LSApplicationQueriesSchemes (11认同)

Jas*_*our 6

单击按钮(实际上)时,使用以下代码调用函数“ 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)
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑LSApplicationQueriesSchemes的info.plist:

在此处输入图片说明

希望会有所帮助!