Google Maps:通用链接格式-目标坐标-“不支持的链接Google Maps无法打开此链接”

Ric*_*hiy 5 google-maps urlencode deep-linking swift ios-universal-links

什么是实现以下目的的正确URL格式:

  1. 使用Universal Link从iOS上的其他应用程序打开Goog​​le Maps应用程序。
  2. 根据两个坐标(纬度经度)设置目的地,并让用户选择运输方式。

什么不起作用:

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)
Run Code Online (Sandbox Code Playgroud)

另外,我尝试使用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)
Run Code Online (Sandbox Code Playgroud)

在两种情况下,结果都是相同的:“不支持的链接-Google Maps无法打开此链接” 在此处输入图片说明

另一方面,如果我删除Google Maps应用程序,或在模拟器中尝试相同的代码,则一切工作正常,并且可以完全达到我想要的结果:
在此处输入图片说明

为什么同一链接可以成功启动“ Web”应用程序,但是无法被本机应用程序识别?

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

我要遵循的指南:Google Maps Guide

Dan*_*yev 5

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

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

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

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)
  }
}
Run Code Online (Sandbox Code Playgroud)