How to open links in Apple Maps or Google Maps?
Asked Answered
P

2

25

How can links on websites be made to lead to, firstly, a location in the new Google Maps for iOS app, and if not available, revert to Apple Maps?

Currently, the default functionality is that a link to maps.apple.com will be opened in Apple Maps on iOS, and Google Maps on other devices.

It'd be good to reverse that on iOS since Google Maps seem to be preferred by most users.

Pyretic answered 18/12, 2012 at 1:6 Comment(1)
@Abizern, let's not turn this into a debate — look at reviews, news, and discussion forums. Also, it's undeniable that most users like accurate map data, an intelligent search function, and reliable routing (with public transport included)Pyretic
L
38

I do not think a single URL is going to be able to do that.

The Google Maps for iOS uses the comgooglemaps:// schema:

https://developers.google.com/maps/documentation/ios/urlscheme

And the Apple Maps app uses http://maps.apple.com:

https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html#//apple_ref/doc/uid/TP40007899-CH5-SW1

I think the best you are going to be able to do is give the user the choice, or just run with Apple maps since you'll know it's there (if there user-agent is iOS 6).

Laddie answered 19/12, 2012 at 3:58 Comment(5)
I see, so there's no single fix? Would be great if there was a universal link that defaults to the phone's map application...Pyretic
If you were writing a native iOS app there would be a simple, single fix. You'd just have to test if the device supports the comgooglemaps:// schema... unfortunately you cannot do this from a website.Laddie
Just a little note, you probably don't have to check the user agent when using maps.apple.com, as it will automatically redirect to the corresponding url on Google Maps if it detects you're not on a supported device.Quicksand
Android supports the geo: scheme, but iOS doesn't. Thus, on Android devices you could use geo:lat,long?q=address as that triggers the map intent - allowing the user to open the address with their preferred map application. I personally don't select a preferred one, as that way I'm able to select Uber or Moovit or Maps, depending on my need :)Dauphin
This information is outdated. https://maps.google.com opens in Apple Maps, unless Google Maps is available, then it opens in Google Maps (it asks for it on the first time). Nowadays, Apple Maps can be uninstalled, so it's not a safe option. What I wonder though, where to change that preference in iOS ...Istic
P
0

You can use canOpenURL(_ url: URL) -> Bool method from a UIApplication instance to have the app try first your preferred option with Google Maps, and if not possible, try Apple Maps. Note that Apple Maps could also not be installed.

Example in Swift 5

// Guard your URL instances.
guard let googleMapsUrl = URL(string: "https://www.google.com/maps/@42.585444,13.007813,6z"),
      let appleMapsUrl = URL(string: "http://maps.apple.com/?q=Mexican+Restaurant") else {
          print("Error creating URLs")
          return
    }

// Check if link can be opened with Google Maps.
guard UIApplication.canOpenURL(googleMapsUrl) else {
    // Use apple maps.
    UIApplication.shared.open(appleMapsUrl, options: [:], completionHandler: nil)
    return
}

// Use google maps.
UIApplication.shared.open(googleMapsUrl, options: [:], completionHandler: nil)

Note

The code provided as example in the Google Maps documentation here: https://developers.google.com/maps/documentation/urls/ios-urlscheme is deprecated. You should use:

UIApplication.shared.open("https://www.url,com", options: [:], completionHandler: nil)

Refernece: https://developer.apple.com/documentation/uikit/uiapplication/1622961-openurl

Pratt answered 5/4, 2022 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.