如何用掉落的针脚打开谷歌地图应用程序? - 斯威夫特

时间:2017-04-07 11:02:18

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

我有这个代码打开具有特定位置的Google地图应用程序并且它正常工作但我需要的是在该位置放置一个引脚,是否可能?

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://");
            }

如何做到这一点?

9 个答案:

答案 0 :(得分:19)

  

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://")
    }
}

答案 1 :(得分:5)

首先打开startsWith作为源代码(Righ单击文件Info.plis,然后选择源代码)并添加:

Info.plist

接下来,为了更轻松地打开Goog​​le地图,请创建如下功能:

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

每当您想要使用给定坐标打开Goog​​le地图时,只需调用函数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://") } }

有关支票Maps URLsMaps SDK for iOS

的更多信息

答案 2 :(得分: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)
    }

如果已在默认的Apple地图中打开,则使用此代码在谷歌地图中打开。

答案 3 :(得分:2)

  

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

答案 4 :(得分:1)

您可以使用此:

FROM microsoft/dotnet-framework:3.5

ARG ENTRY
ENV my_env=$ENTRY

#RUN echo %ENTRY%
#RUN echo %my_env%

WORKDIR C:\\test
ADD ["/bin/x86/Release/","C:/test/"]

ENTRYPOINT C:/test/file.exe %my_env%

答案 5 :(得分:0)

您应将此行添加到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")!)
        }
    }

答案 6 :(得分:0)

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

答案 7 :(得分:0)

这在2019年更容易实现:只需使用通用链接

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

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

答案 8 :(得分:0)

这个简单的URL方案对我有用

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

P.S。但不要忘记将comgooglemaps URL方案添加到Info.plist