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

1

7

What is the correct URL format to achieve the following:

  1. Open a Google Maps app from another app on iOS using the Universal Link.
  2. Set destination based on two coordinates: latitude and longitude and let the user select the transportation method.

What doesn't work:

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)

Also, I've tried to encode the URL using addingPercentEncoding method:

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)

The result in both cases is the same: "Unsupported link - Google Maps can't open this link" enter image description here

On the other hand, if I delete the Google Maps app, or try the same code in the simulator, everything works fine and I achieve exactly the result I aim for:
enter image description here

Why does the same link successfully launches the "Web" app, but fails to be recognized by the native app?

String in question:
https://www.google.com/maps/dir/?api=1&destination=-20.021999,57.579075

The guide I'm following: Google Maps Guide

Nuclide answered 9/7, 2018 at 11:54 Comment(2)
Check this #32040316Ego
@A.K.M that answer is not applicable for universal links.Nuclide
C
6

I've just tried your second approach (with addingPercentEncoding(withAllowedCharacters:) and provided the given coordinate and got exactly the same error in Google Maps app.

But then I changed coordinate to the one that is in my city and voila! it worked. Seems like google maps simply couldn't generate directions given the (-20.021999, 57.579075) pair. And judging by your second screenshot (with web version presented – it is blank, no map is presented) I think it's struggling with generating directions in your case too. Why does it tell that the link is not supported in such case puzzles me though.

Here's the full code (I've created a blank single-view app):

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)
  }
}
Casey answered 9/7, 2018 at 19:4 Comment(2)
Dan, very interesting. Clearly, the destination point I provided is in Mauritius while the phone used during the test was located in Europe. Could you provide the full sample code opening the Google Maps for you?Nuclide
While your coordinated didn't work for me either, I've created another pair, closer to my actual location and the linking worked. Seems to be a Google Maps bug.Nuclide

© 2022 - 2024 — McMap. All rights reserved.