19

Google gave an example http://googlemapsapi.blogspot.com/2007/05/driving-directions-support-added-to.html

Is the source code available somewhere or a tutorial on that precise example ?

Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
user310291
  • 36,946
  • 82
  • 271
  • 487

2 Answers2

73

Here's a very basic example using the v3 API:

<!DOCTYPE html>
    <html> 
    <head> 
       <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
       <title>Google Maps API v3 Directions Example</title> 
       <script type="text/javascript" 
               src="http://maps.google.com/maps/api/js?sensor=false"></script>
    </head> 
    <body style="font-family: Arial; font-size: 12px;"> 
       <div style="width: 600px;">
         <div id="map" style="width: 280px; height: 400px; float: left;"></div> 
         <div id="panel" style="width: 300px; float: right;"></div> 
       </div>
       
       <script type="text/javascript"> 
    
         var directionsService = new google.maps.DirectionsService();
         var directionsDisplay = new google.maps.DirectionsRenderer();
    
         var map = new google.maps.Map(document.getElementById('map'), {
           zoom:7,
           mapTypeId: google.maps.MapTypeId.ROADMAP
         });
        
         directionsDisplay.setMap(map);
         directionsDisplay.setPanel(document.getElementById('panel'));
    
         var request = {
           origin: 'Chicago', 
           destination: 'New York',
           travelMode: google.maps.DirectionsTravelMode.DRIVING
         };
    
         directionsService.route(request, function(response, status) {
           if (status == google.maps.DirectionsStatus.OK) {
             directionsDisplay.setDirections(response);
           }
         });
       </script> 
    </body> 
    </html>

Screenshot:

Google Maps API v3 Directions Example

Ani Menon
  • 27,209
  • 16
  • 105
  • 126
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
  • 2
    Thank you that's exactly what I wanted: the minimalistic example for learning. – user310291 Oct 10 '10 at 08:12
  • hi can i add more that one destination for the same source and get route directions for all the destinations at a single time.. – manju Feb 07 '11 at 05:11
  • @danial vassallo Thanx for this helpful answer. does this have language option for the response output that it gives.? – hardik Jul 06 '12 at 16:57
  • Hi Daniel, I had implemented your answer, it works fine, i modified it little bit, i added **alternative:true** in request, so it giving me multiple routes, and if i clicked on one of the route, it immediately show route on Map, now i want to know that and which route user has clicked ? is there any way to save the details of that route ? can u please suggest me something ? – Deepu Dec 24 '12 at 08:17
  • @hardik To change language, you need to modify the call to the maps API ie for French: http://maps.google.com/maps/api/js?sensor=false&language=fr – Fraser Jul 22 '13 at 15:24
  • @Fraser thanks fraser for that answer. actually it was an issue a long time ago, but thanks for the comment here. appreciate. – hardik Jul 23 '13 at 09:14
  • 1
    @hardik no probs. I saw it was a while back but thought I'd offer help for anyone else coming to the post if they needed it. Cheers – Fraser Jul 23 '13 at 09:37
  • @Daniel Vassallo: I want to show the text based directions which you showed in attached screen shot in iOS Mobile App. Can you please provide me the API where can i get that response. – Sudheer Kumar Palchuri Apr 01 '15 at 04:08
  • I spent FOREVER trying to get around the silly CORS problem. This was the UTLIMATE solution. Thank you so much. – Clever Programmer Jan 08 '16 at 02:51
  • can any one say that how can I add lontitude /latitude to replace city name here t replace city name, according to above example var request = { origin: 'Chicago', destination: 'New York', travelMode: google.maps.DirectionsTravelMode.DRIVING }; – user3732708 Feb 23 '16 at 05:43
  • You can read the latest about directions panels here: https://developers.google.com/maps/documentation/javascript/examples/directions-panel – Pete Jan 09 '17 at 22:02
  • In the same code, I want to show direction from my current location to a lat long, Also when I move then my marker should move. Same like google map app. Is it possible? If yes please tell me how can I do it with java-script and php. I have google map driving direction API key. – shiv Dec 13 '18 at 06:03
3

Code to get route, legs and other data using Latitude/longitude OR using Address without google map in JavaScript using v3 API:

<script src="https://maps.googleapis.com/maps/api/js?v=3&key=<key>&libraries=places"></script>

<script>
    var directionsService = new google.maps.DirectionsService();

    var start_lat = "41.8781136";
    var start_lon = "-87.6297982";
    var end_lat = "40.7127753";
    var end_lon = "-74.0059728";

    // Using Latitude and longitude
    var request = {
        origin: new google.maps.LatLng(start_lat, start_lon),
        destination: new google.maps.LatLng(end_lat, end_lon),
        optimizeWaypoints: true,
        avoidHighways: false,
        avoidTolls: false,
        travelMode: google.maps.TravelMode.DRIVING
    };

    //Using plain address
    // var request = {
    //     origin: { query: "Chicago, IL, USA" },
    //     destination: {  query: "New York, NY, USA" },
    //     travelMode: google.maps.TravelMode.DRIVING,
    // };

    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            var route = response.routes[0];
            var leg = response.routes[0].legs[0];
            var polyline = route.overview_polyline;
            var distance = route.legs[0].distance.value;
            var duration = route.legs[0].duration.value;

            console.log(route); // Complete route
            console.log(distance); // Only distance 
            console.log(duration); // Only duration
            console.log(leg); // Route options/list
            console.log(polyline); // Polyline data
        }
    });

</script>
Nadeem0035
  • 3,757
  • 1
  • 24
  • 36