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?

前端 未结 10 559
无人及你
无人及你 2021-01-29 17:33

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 lin

10条回答
  •  温柔的废话
    2021-01-29 18:27

    just bumped in this question and found here all the answers I took some of the codes above and made simple js function that works on android and iphone (it supports almost every android and iphones).

      function navigate(lat, lng) {
        // If it's an iPhone..
        if ((navigator.platform.indexOf("iPhone") !== -1) || (navigator.platform.indexOf("iPod") !== -1)) {
          function iOSversion() {
            if (/iP(hone|od|ad)/.test(navigator.platform)) {
              // supports iOS 2.0 and later
              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];
    
          var protocol = 'http://';
          if (ver[0] >= 6) {
            protocol = 'maps://';
          }
          window.location = protocol + 'maps.apple.com/maps?daddr=' + lat + ',' + lng + '&ll=';
        }
        else {
          window.open('http://maps.google.com?daddr=' + lat + ',' + lng + '&ll=');
        }
      }
    

    The html:

     Israel
    

提交回复
热议问题