Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a link for all mobile devices that opens google maps with a route starting at the current location, destinating a given place?

I rather thought this would not be so hard to find out but appearantly it is not easy to find an awesome cross device article, like you'd expect.

I want to create a link which opens either the mobile device's browser and surf to google maps OR open a maps app (Apple Maps or Google Maps) and directly starting a route, i.e.: start at the current location, end at a given point (lat/long).

I can test on two devices (beside browserstack), an Android and an iPhone.

The following link works only on the Android:

<a href="http://maps.google.com/maps?daddr=lat,long&amp;ll=">Take me there!</a> 

Clicking this link in iPhone's Chrome, this weirdly opens Google Maps in desktop version with ads on the mobile app...

This one only works on iOS, opening Apple Maps asking me to enter a start location (i can pick "Current Location") and start the route = desired behavior. Clicking this link completely fails on Android:

<a href="maps://maps.google.com/maps?daddr=lat,long&amp;ll=">Take me there!</a>

Notice the maps:// protocol.

Is there an elegant cross device way of creating such a link? One link that works on all main mobiles?

Thanks

UPDATE: Solution found (kinda)

Here is what I've come up with. It's not quite what I imagined, though it's working.

var ua = navigator.userAgent.toLowerCase(),     plat = navigator.platform,     protocol = '',     a,     href;  $.browser.device = ua.match(/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera/i) ? ua.match(/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera/i)[0] : false;   if ($.browser.device) {     switch($.browser.device) {         case 'iphone':         case 'ipad':         case 'ipod':             function iOSversion() {               if (/iP(hone|od|ad)/.test(navigator.platform)) {                 // supports iOS 2.0 and later: <http://bit. ly/TJjs1V>                 var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/);                 return [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)];               }             }              var ver = iOSversion() || [0];              if (ver[0] >= 6) {               protocol = 'maps://';             }             else {                 protocol = 'http://maps.google.com/maps';             }         break;          case 'android':         default:             protocol = 'http://maps.google.com/maps';         break;     }  a.attr('href', protocol + href) 

the maps:// protocol is the url scheme for the apple maps app, which will only start working on ios 6 or higher. There are ways to test if gmaps is installed and then chose what to do with the url, but that was kind of too much for what I intended. So i just ended up creating a maps:// OR maps.google.com/ link, using the above parameters.

** UPDATE **

sadly, $.browser.device don't work since jquery 1.9 (source - http://api.jquery.com/jquery.browser )

like image 747
Alex Avatar asked Sep 11 '13 11:09

Alex


People also ask

How do I share a Google map between devices?

Sign in to your Google account if you haven't already. Tap Location sharing and Share location. Select how long you want to share your location. Choose the people you want to share your location with (you may have to give Google Maps access to your contacts here).

How do I redirect a route on Google Maps?

To choose an alternate route, either click on a greyed-out route on the map or click on one of the other routes listed on the left-hand side menu. Note that you can also change routes by clicking on one and dragging it so that the directions will take you via certain roads.


2 Answers

I haven't worked much with phones, so I dont't know if this would work. But just from a html/javascript point of view, you could just open a different url depending on what the user's device is?

<a style="cursor: pointer;" onclick="myNavFunc()">Take me there!</a>  function myNavFunc(){     // If it's an iPhone..     if( (navigator.platform.indexOf("iPhone") != -1)          || (navigator.platform.indexOf("iPod") != -1)         || (navigator.platform.indexOf("iPad") != -1))          window.open("maps://www.google.com/maps/dir/?api=1&travelmode=driving&layer=traffic&destination=[YOUR_LAT],[YOUR_LNG]");     else          window.open("https://www.google.com/maps/dir/?api=1&travelmode=driving&layer=traffic&destination=[YOUR_LAT],[YOUR_LNG]"); } 
like image 122
James Avatar answered Oct 26 '22 21:10

James


Interestingly, http://maps.apple.com links will open directly in Apple Maps on an iOS device, or redirect to Google Maps otherwise (which is then intercepted on an Android device), so you can craft a careful URL that will do the right thing in both cases using an "Apple Maps" URL like:

http://maps.apple.com/?daddr=1600+Amphitheatre+Pkwy,+Mountain+View+CA

Alternatively, you can use a Google Maps url directly (without the /maps URL component) to open directly in Google Maps on an Android device, or open in Google Maps' Mobile Web on an iOS device:

http://maps.google.com/?daddr=1+Infinite+Loop,+Cupertino+CA

like image 35
Alex Pretzlav Avatar answered Oct 26 '22 21:10

Alex Pretzlav