【问题标题】:How to open google maps app with a dropped pin ? - Swift如何使用掉落的图钉打开谷歌地图应用程序? - 斯威夫特
【发布时间】:2017-04-07 11:02:18
【问题描述】:

我有这段代码可以打开具有特定位置的谷歌地图应用程序,它正在工作,但我需要在该位置放置一个图钉,这可能吗?

if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {

                UIApplication.shared.openURL(URL(string:
                    "comgooglemaps://?center=\(self.location.coordinate.latitude),\(self.location.coordinate.longitude)&zoom=14&views=traffic")!)
            } else {
                print("Can't use comgooglemaps://");
            }

如何做到这一点?

【问题讨论】:

    标签: ios swift google-maps google-maps-sdk-ios


    【解决方案1】:

    q:搜索的查询字符串。

    它在给定坐标中创建一个标记。试试这个

    if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
            UIApplication.shared.open(URL(string:"comgooglemaps://?center=\(self.location.coordinate.latitude),\(self.location.coordinate.longitude)&zoom=14&views=traffic&q=\(self.location.coordinate.latitude),\(self.location.coordinate.longitude)")!, options: [:], completionHandler: nil)
        } else {
            print("Can't use comgooglemaps://")
        }
    }
    

    【讨论】:

    • 我以为q 只用于搜索,但我记得你可以按坐标搜索,谢谢你成功了。
    • 在标记中显示位置名称而不是坐标的任何方式?
    • Vinoth Vino,你找到办法了吗?
    【解决方案2】:

    首先打开Info.plist作为Source Code(右击文件Info.plis,然后选择Source Code)并添加:

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

    接下来为了更容易打开谷歌地图,创建如下函数:

    func openGoogleMaps() {
        if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
            UIApplication.shared.openURL(URL(string:
                "comgooglemaps://?center=\(myLatitude),\(myLongitude)&zoom=14&views=traffic")!)
        } else {
            print("Can't use comgooglemaps://")
        }
    }
    

    只要你想打开给定坐标的谷歌地图,你只需调用函数openGoogleMaps()

    更多信息请查看Maps URLsMaps SDK for iOS

    【讨论】:

      【解决方案3】:
      if UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
              UIApplication.shared.open(URL(string: "comgooglemaps://?center=\(self.latitude),\(self.longitude)")!, options: [:], completionHandler: nil)
          } else {
               print("Opening in Apple Map")
      
          let coordinate = CLLocationCoordinate2DMake(self.latitude, self.longitude)
          let region = MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(0.01, 0.02))
          let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
          let mapItem = MKMapItem(placemark: placemark)
          let options = [
              MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: region.center),
              MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: region.span)]
          mapItem.name = theLocationName
          mapItem.openInMaps(launchOptions: options)
          }
      

      如果已安装,请使用此代码在谷歌地图中打开,否则在默认苹果地图中打开。

      【讨论】:

        【解决方案4】:

        这里是 Swift 5 解决方案,如果未安装 Google Maps 应用程序,您可以确保地图显示在浏览器中,如果安装在 Google Maps 应用程序中,则可以确保地图显示在浏览器中设备,这两种情况都带有位置的引脚。

         if UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
             UIApplication.shared.open(URL(string:"comgooglemaps://?center=\(latitude),\(longitude)&zoom=14&views=traffic&q=\(latitude),\(longitude)")!, options: [:], completionHandler: nil)
         } else {
             UIApplication.shared.open(URL(string: "http://maps.google.com/maps?q=loc:\(latitude),\(longitude)&zoom=14&views=traffic&q=\(latitude),\(longitude)")!, options: [:], completionHandler: nil)
         }
        

        【讨论】:

          【解决方案5】:

          Swift 3,iOS 10

          let lat = 0.0
          let lon = 0.0
          
          if UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!) {
              UIApplication.shared.open(URL(string: "comgooglemaps://?center=\(lat),\(lon)&zoom=14&views=traffic&q=loc:\(lat),\(lon)")!, options: [:], completionHandler: nil)
          } else {
              print("Can't use comgooglemaps://")
              UIApplication.shared.open(URL(string: "http://maps.google.com/maps?q=loc:\(lat),\(lon)&zoom=14&views=traffic")!, options: [:], completionHandler: nil)
          }
          

          【讨论】:

            【解决方案6】:

            你可以用这个:

            if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
                UIApplication.shared.openURL(URL(string:"comgooglemaps://?center=\(self.location.coordinate.latitude),\(self.location.coordinate.longitude)&zoom=14&views=traffic&q=\(self.location.coordinate.latitude),\(self.location.coordinate.longitude)")!)
            } else {
                print("Can't use comgooglemaps://")
            }
            

            【讨论】:

              【解决方案7】:

              这在 2019 年要简单得多:只需使用通用链接

              URL(string: "https://www.google.com/maps/search/?api=1&query=\(lat),\(lng)")
              

              请参阅https://developers.google.com/maps/documentation/urls/guide 上的完整文档。

              【讨论】:

                【解决方案8】:

                你应该在 info.Plist 文件中添加这一行

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

                代码在这里:

                if dicDetails.hasValueForKey("latitude") && dicDetails.hasValueForKey("latitude") {
                        if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
                            if #available(iOS 10.0, *) {
                                UIApplication.shared.open(URL(string:"comgooglemaps://?center=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude"))&zoom=14&views=traffic&q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude"))")!, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil)
                            } else {
                                // Fallback on earlier versions
                                 //https://www.google.com/maps/@
                                UIApplication.shared.openURL(URL(string:"https://www.google.com/?q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude")),15z")!)
                            }
                        } else {
                            //print("Can't use comgooglemaps://")
                            UIApplication.shared.openURL(URL(string:"https://www.google.com/?q=\(dicDetails.stringValueForKey("latitude")),\(dicDetails.stringValueForKey("longitude")),15z")!)
                        }
                    }
                

                【讨论】:

                  【解决方案9】:

                  这个简单的 URL 方案适合我

                  URL(string: "comgooglemaps://?q=\(lat),\(lng)")
                  

                  附:但不要忘记将comgooglemaps URL 方案添加到 Info.plist

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2015-09-17
                    • 1970-01-01
                    • 1970-01-01
                    • 2021-06-15
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多