【问题标题】:Google Maps: Universal link format - destination coordinates - "Unsupported Link Google Maps can't open this link"谷歌地图:通用链接格式 - 目的地坐标 - “不支持的链接谷歌地图无法打开此链接”
【发布时间】:2018-07-09 11:54:15
【问题描述】:

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

  1. 使用 Universal Link 从 iOS 上的其他应用打开 Google 地图应用。
  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)

另外,我尝试使用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 Guide

【问题讨论】:

标签: swift google-maps deep-linking urlencode ios-universal-links


【解决方案1】:

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

但后来我将坐标更改为我所在城市的坐标,瞧!有效。鉴于 (-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)
  }
}

【讨论】:

  • 丹,很有趣。显然,我提供的目的地是毛里求斯,而测试期间使用的手机位于欧洲。您能否提供为您打开 Google 地图的完整示例代码?
  • 虽然您的协调对我也不起作用,但我已经创建了另一对,更接近我的实际位置并且链接有效。似乎是谷歌地图的错误。
猜你喜欢
  • 2019-12-18
  • 2012-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
相关资源
最近更新 更多