实现以下目标的正确 URL 格式是什么:
什么不起作用:
let encoded = "https://www.google.com/maps/dir/?api=1&destination=-20.021999%2C57.579075"
let url = URL(string: encoded)!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
此外,我尝试使用 addingPercentEncoding 方法对 URL 进行编码:
let string = "https://www.google.com/maps/dir/?api=1&destination=\(lat),\(long)"
let encoded = string.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let url = URL(string: encoded)!
UIApplication.shared.open(url, options: [:], completionHandler: nil)
两种情况下的结果是一样的:
“不支持的链接 - Google map 无法打开此链接”
另一方面,如果我删除 Google map 应用程序,或在模拟器中尝试相同的代码,一切正常,并且我达到了我想要的结果:
为什么同样的链接成功启动了“Web”应用,但无法被原生应用识别?
有问题的字符串:
https://www.google.com/maps/dir/?api=1&destination=-20.021999,57.579075
我关注的指南:Google Maps Guide
最佳答案
我刚刚尝试了您的第二种方法(使用 addingPercentEncoding(withAllowedCharacters:) 并提供了给定的坐标,但在 Google map 应用中出现了完全相同的错误。
但后来我将坐标更改为我所在城市的坐标,瞧!有效。似乎谷歌地图根本无法在给定 (-20.021999, 57.579075) 对的情况下生成方向。从您的第二个屏幕截图(显示了网络版本 - 它是空白的,没有显示 map )来看,我认为它也在为您的情况生成方向而苦苦挣扎。为什么它告诉我在这种情况下不支持链接,这让我很困惑。
这是完整的代码(我创建了一个空白的单 View 应用):
import UIKit
class ViewController: UIViewController {
override func loadView() {
super.loadView()
let lat = 51.150992
let long = 71.426444
let string = "https://www.google.com/maps/dir/?api=1&destination=\(lat),\(long)"
let encoded = string.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let url = URL(string: encoded)!
UIApplication.shared.open(url)
}
}
关于swift - 谷歌地图 : Universal link format - destination coordinates - "Unsupported Link Google Maps can' t open this link",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51244976/