如何将地址从应用程序传递到谷歌地图,而不仅仅是swift中的纬度和经度

如何将地址从应用程序传递到谷歌地图,而不仅仅是swift中的纬度和经度,swift,google-maps,Swift,Google Maps,我是swift的新手,在谷歌地图上显示地址时遇到了问题。我只能显示纬度和经度 if(UIApplication.shared.canOpenURL(URL(字符串:“comgooglemaps:/”)){ UIApplication.shared.openURL(NSURL(字符串:“comgooglemaps://?SADD=&daddr=\(Float(self.lat)!)、\(Float(self.lon)!)和directionsmode=driving)!作为URL) } 此代码

我是swift的新手,在谷歌地图上显示地址时遇到了问题。我只能显示纬度和经度

if(UIApplication.shared.canOpenURL(URL(字符串:“comgooglemaps:/”)){
UIApplication.shared.openURL(NSURL(字符串:“comgooglemaps://?SADD=&daddr=\(Float(self.lat)!)、\(Float(self.lon)!)和directionsmode=driving)!作为URL)
}

此代码显示如图所示的地址

下面是代码

    let firstAddress = "your, local address, here"

    let testURL: NSURL = NSURL(string: "comgooglemaps-x-callback://")!

    if UIApplication.shared.canOpenURL(testURL as URL) {
        if let address = firstAddress.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) {
            let directionsRequest: String = "comgooglemaps-x-callback://" + "?daddr=\(address)" + "&x-success=sourceapp://?resume=true&directionsmode=driving"
            let directionsURL: NSURL = NSURL(string: directionsRequest)!
            let optionsKeyDictionary = [UIApplication.OpenExternalURLOptionsKey(rawValue: "universalLinksOnly"): NSNumber(value: true)]
            UIApplication.shared.open(directionsURL as URL, options: optionsKeyDictionary, completionHandler: nil)
        }
    }
    else {
        print("Can't use comgooglemaps-x-callback:// on this device.")
    }
您还需要在Info.plist中添加以下内容

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>comgooglemaps-x-callback</string>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>comgooglemaps</string>
</array>
LSApplicationQueriesSchemes
comgooglemaps-x-callback

您可以将查询参数
q
comgooglemaps
方案一起使用,如下所示

let address = "" //Replace with address to open

//Replace line breaks and white spaces in the address with queryable values
let queryableAddress = address.replacingOccurrences(of: "\n", with: " ").replacingOccurrences(of: " ", with: "%20")

if let url = URL(string: "comgooglemaps://?q=\(queryableAddress)"), UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

它将打开谷歌地图,如果安装在您的设备中,并在您的项目中添加了url方案,否则将打开默认的苹果地图,并提供纬度和经度

func openMapWithLatLng(dLat: Double, dLng: Double) {
    if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(URL(string:"comgooglemaps://?daddr=\(dLat),\(dLng)&directionsmode=driving&zoom=14&views=traffic")!, options: [:]
                , completionHandler: nil)
        } else {
            UIApplication.shared.openURL(URL(string:"comgooglemaps://?daddr=\(dLat),\(dLng)&directionsmode=driving&zoom=14&views=traffic")!)
        }
    }else {
        let url = "http://maps.apple.com/maps?daddr=\(dLat),\(dLng)"

        if #available(iOS 10.0, *) {
            UIApplication.shared.open(URL(string: url)!, options: [:]
                , completionHandler: nil)
        } else {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(URL(string: url)!, options: [:]
                    , completionHandler: nil)
            } else {
                // Fallback on earlier versions
            }
        }
    }
}
在您的Info.plist中添加以下方案

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>comgooglemaps-x-callback</string>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>comgooglemaps</string>
</array>
LSApplicationQueriesSchemes
谷歌地图