代码之家  ›  专栏  ›  技术社区  ›  Richard Topchii

谷歌地图:通用链接格式-目标坐标-“不支持的链接谷歌地图无法打开此链接”

  •  3
  • Richard Topchii  · 技术社区  · 8 年前

    要实现以下目标,正确的URL格式是什么?

    1. 使用 万向节 .
    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 方法:

    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)
    

    两种情况下的结果相同: “不支持的链接-谷歌地图无法打开此链接” enter image description here

    另一方面,如果我删除了谷歌地图应用程序,或者在模拟器中尝试相同的代码,一切都会很好地工作,我完全达到了我的目标:
    enter image description here

    为什么同一个链接成功地启动了“web”应用程序,但无法被本机应用程序识别?

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

    我下面的指南: Google Maps Guide

    1 回复  |  直到 8 年前
        1
  •  1
  •   Dan Karbayev    8 年前

    我刚试过你的第二种方法 addingPercentEncoding(withAllowedCharacters:) 并提供了给定的坐标,在谷歌地图应用程序中得到了完全相同的误差。

    但后来我把坐标改成了我所在城市的坐标,瞧!它起作用了。似乎谷歌地图无法生成给定(-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)
      }
    }
    
    推荐文章