Google 地图:通用 link 格式 - 目的地坐标 - "Unsupported Link Google Maps can't open this link"

Google Maps: Universal link format - destination coordinates - "Unsupported Link Google Maps can't open this link"

什么是正确的URL格式来实现如下:

  1. 使用 Universal Link.
  2. 从 iOS 上的另一个应用打开 Google 地图应用
  3. 根据两个坐标设置目的地纬度经度,让用户select交通方式

什么不起作用:

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)

两种情况下的结果是一样的: "Unsupported link - Google Maps can't open this link"

另一方面,如果我删除 Google 地图应用程序,或在模拟器中尝试相同的代码,一切正常,我实现了我想要的结果:

为什么同样的link成功启动了"Web"应用,却无法被原生应用识别?

有问题的字符串:
https://www.google.com/maps/dir/?api=1&destination=-20.021999,57.579075

我关注的指南:Google Maps Guide

我刚刚尝试了您的第二种方法(使用 addingPercentEncoding(withAllowedCharacters:) 并提供了给定的坐标,但在 Google 地图应用程序中得到了完全相同的错误。

但后来我将坐标更改为我所在城市的坐标,瞧!有效。似乎 google 地图在给定 (-20.021999, 57.579075) 对的情况下根本无法生成方向。从您的第二个屏幕截图(显示了网络版本 - 它是空白的,没有显示地图)来看,我认为它也在为您的情况生成方向而苦苦挣扎。为什么它告诉我在这种情况下不支持 link 虽然我很困惑。

这是完整代码(我创建了一个空白的单视图应用):

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