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 556
无人及你
无人及你 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:

     <a onclick="navigate(31.046051,34.85161199999993)" >Israel</a>
    
    0 讨论(0)
  • 2021-01-29 18:29

    Uhmm, I haven't worked much with phones so I dunno if this would work but just from a html/javascript point of view could you 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]");
    }
    
    0 讨论(0)
  • 2021-01-29 18:37

    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

    0 讨论(0)
  • 2021-01-29 18:39
    if (navigator.geolocation) { //Checks if browser supports geolocation
    navigator.geolocation.getCurrentPosition(function (position) {                                                                                            
     var latitude = position.coords.latitude;                    //users current
     var longitude = position.coords.longitude;                 //location
     var coords = new google.maps.LatLng(latitude, longitude); //Creates variable for map coordinates
     var directionsService = new google.maps.DirectionsService();
     var directionsDisplay = new google.maps.DirectionsRenderer();
     var mapOptions = //Sets map options
     {
       zoom: 15,  //Sets zoom level (0-21)
       center: coords, //zoom in on users location
       mapTypeControl: true, //allows you to select map type eg. map or satellite
       navigationControlOptions:
       {
         style: google.maps.NavigationControlStyle.SMALL //sets map controls size eg. zoom
       },
       mapTypeId: google.maps.MapTypeId.ROADMAP //sets type of map Options:ROADMAP, SATELLITE, HYBRID, TERRIAN
     };
     map = new google.maps.Map( /*creates Map variable*/ document.getElementById("map"),    mapOptions /*Creates a new map using the passed optional parameters in the mapOptions parameter.*/);
     directionsDisplay.setMap(map);
     directionsDisplay.setPanel(document.getElementById('panel'));
     var request = {
       origin: coords,
       destination: 'BT42 1FL',
       travelMode: google.maps.DirectionsTravelMode.DRIVING
     };
    
     directionsService.route(request, function (response, status) {
       if (status == google.maps.DirectionsStatus.OK) {
         directionsDisplay.setDirections(response);
       }
     });
     });
     }
    
    0 讨论(0)
提交回复
热议问题