Detecting geo-uri-support
Asked Answered
P

2

18

Is there a way with JavaScript to check whether the client-browser supports the GEO-URI-scheme?

I want to show a coordinate for nonsupported browsers as a link to a map

<a href='http://osm.org/#map=18/52.51627/13.37769'>Berlin</a>

and for supported browsers (like smartphones) as a link to their own native apps.

<a href='geo:52.51627,13.37769'>Berlin</a>
Podagra answered 5/11, 2013 at 10:44 Comment(0)
E
8

No, there isn't. Wether or not the geo protocol is available isn't up to the browser, but the underlying OS. Android's browser has no idea if it can support it, it asks the subsystem if it has a handler for the geo URI, and if so, open it. The only way to know that the system supports a custom uri would be to try and open a link, and seeing if it works. There wouldn't be a way to do this automatically without completely interrupting the user.

A good example on the best way to do this would be Apple's maps.apple.com. They use server-side UA sniffing to redirect maps.apple.com to the best site available.

Trying to go to http://maps.apple.com/?q=cupertino on any mavericks browser will result in them sending a 302 to a maps:// url (the custom URI for the Maps.app app). Same for iOS devices. If it is an unrecognized browser, it will redirect to the corresponding maps.google.com address, since they don't have their own maps web presence.

Elliottellipse answered 13/11, 2013 at 6:59 Comment(5)
There's a promising polyfill (depending on Dojo) out there, also relying on client-side UA sniffing. It might make sense to use Apple Maps Links as a fallback instead of Google Maps, though.Buddhism
is there a difference from apple maps links and what I suggested?Elliottellipse
No it didn't? I say everything you mentionElliottellipse
I totally get that - I just don't understand what you added. All of the facts you mention are things in my answer.Elliottellipse
The link to dojo-geo-uri-polyfill and the comment on how this should better be modified (by using Apple Maps instead of Google Maps as a fallback). Why exactly do we discuss this in that length? I'm going to delete all follow up comments to make it more graspable for anyone else again.Buddhism
R
0

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Geo URI Berlin</title>
</head>
<body>
<script>
function openGeo(geo) {
  try { 
    document.location = "geo:"+geo;
  } catch(e) {
    document.location = "https://www.openstreetmap.org/?mlat="+geo.replace(",","&mlon=")+"#map=18/"+geo.replace(",","/")
  } 
}
</script>   
<a href="javascript:" onclick="openGeo('52.51627,13.37769');">Berlin</a> 
</body>
</html> 
Rubidium answered 26/10, 2023 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.