实现以下内容,正确的URL格式是什么:
- 在iOS上通过通用链接从另一个应用程序中打开谷歌地图应用。
- 基于两个坐标:纬度和经度设置目的地,并让用户选择交通方式。
不起作用的方法:
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 地图无法打开此链接" 另一方面,如果我删除 Google 地图应用程序,或者在模拟器中尝试相同的代码,一切都正常工作,并且我实现了完全符合预期的结果: 为什么同样的链接可以成功启动 "Web" 应用程序,但无法被本地应用程序识别? 相关字符串:
https://www.google.com/maps/dir/?api=1&destination=-20.021999,57.579075
我正在遵循的指南:Google Maps 指南