Get arrival time google maps api javascript

1

I have a map which calculates with this method of google directionsService.route, with that method a response is returned. I just want to be able to get the time calculated by google.

var directionsDisplay = new google.maps.DirectionsRenderer();
      var directionsService = new google.maps.DirectionsService();
      var request = {
        origin:{lat:latitudUsuario,lng:longitudUsuario},
        destination: {lat:9.95,lng:-67.39},
        travelMode: google.maps.DirectionsTravelMode['DRIVING'],
        unitSystem: google.maps.DirectionsUnitSystem['METRIC'],
        provideRouteAlternatives: true
       };
       directionsService.route(request, function(response, status) {
         if (status == google.maps.DirectionsStatus.OK) {
           directionsDisplay.setMap(map);
           directionsDisplay.setDirections(response);
         }else{
           alert("No existen rutas entre ambos puntos");
        }
      });
    
asked by Wuilmer Medrano 28.09.2016 в 01:02
source

1 answer

0

Try doing the following. Right below the call:

    directionsDisplay.setDirections(response);

Add this code:

    // Obtener la duración para la primer ruta
    var route = response.routes[0];
    var duration = 0;

    // Iteramos todos los legs de la ruta
    route.legs.forEach(function (leg) {
      // Sumamos la duracion de cada uno
      // La duración esta en segundos.
      duration += leg.duration.value;
    });

    // Por ejemplo: imprimimos el resultado en DOMElement con id 'duracion'
    document.getElementById('duracion').innerHTML = duration + ' segundos';

I leave a demo here: link

More info here: link

    
answered by 12.10.2016 / 20:37
source